0

i am trying to run below snippet of code on my windows server.

@echo off
set BRANCH_NAME_ID=compiler_branch
if %BRANCH_NAME_ID%==compiler_branch ( echo INSIDE COMPILER BRANCH )
echo %BRANCH_SHORT_ID%|findstr /r "^[r][0-9][0-9]*_00$" & IF %ERRORLEVEL% == 0 (  echo IN IF ) ELSE ( echo INFO else)
pause

I was expecting the only output should be INSIDE COMPILER BRANCH because, BRANCH_NAME_ID variable is referring to compiler_branch. But some reason i am also getting IN IF as well.

Ouptut:-

INSIDE COMPILER BRANCH
IN IF
Press any key to continue . . . 

As per the document https://ss64.com/nt/findstr.html i notice below and wrote the script accordingly. But some reason %ERRORLEVEL% is setting to 0 in line3 of my code thought the string is not matching :-

FINDSTR will set %ERRORLEVEL% as follows:

0 A match is found in at least one line of at least one file.
1 If a match is not found in any line of any file, (or if the file is not found at all).
2 Wrong syntax

An invalid switch will only print an error message in error stream.

Am i missing something ?

Compo
  • 36,585
  • 5
  • 27
  • 39
Kiran
  • 75
  • 7
  • Replace `IF %ERRORLEVEL% == 0` with `if not errorlevel 1`. Batch parses the entire logical line, substitutes the **current** values for any `%varname%`, then executes the command, so it is substituting the value of `errorlevel` at the time that the `echo` is parsed. The syntax given will act on the run-time value. – Magoo Nov 10 '22 at 05:32

1 Answers1

0

Because of how the interpreter reads files (see How does the Windows Command Interpreter (CMD.EXE) parse scripts? for a massive amount of info), %ERRORLEVEL% in that line gets replaced with its current value before the line is actually run. In order to have the command run and then have the value checked correctly, put the if statement on its own line.

@echo off
set BRANCH_NAME_ID=compiler_branch
if %BRANCH_NAME_ID%==compiler_branch ( echo INSIDE COMPILER BRANCH )
echo %BRANCH_SHORT_ID%|findstr /r "^[r][0-9][0-9]*_00$"
IF %ERRORLEVEL% == 0 (  echo IN IF ) ELSE ( echo INFO else)
pause

If for some reason you absolutely insist on using & to chain commands together (there is no reason to ever do this and it only makes things worse imo), then you can enable delayed expansion and use !ERRORLEVEL! instead.

@echo off
setlocal enabledelayedexpansion
set BRANCH_NAME_ID=compiler_branch
if %BRANCH_NAME_ID%==compiler_branch ( echo INSIDE COMPILER BRANCH )
echo %BRANCH_SHORT_ID%|findstr /r "^[r][0-9][0-9]*_00$" & IF !ERRORLEVEL! == 0 (  echo IN IF ) ELSE ( echo INFO else)
pause
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Thanks for your response. Actually here there is an issue where this particular line `echo %BRANCH_SHORT_ID%|findstr /r "^[r][0-9][0-9]*_00$"` causes issue which returns `exited non-zero errorlevel=1` . Script is able to proceed further lines after above line.. And my overall script exit status will become non-zero. Seems like `findstr` tries to look for the pattern and if it doesn't match exit status is returning as non zero. Any WAR to resolve this ? – Kiran Nov 10 '22 at 06:24
  • `%ERRORLEVEL%` stays the same until something else changes it, so if the last thing to set it was the `findstr` command, that's what it will return. If you want to reset the errorlevel to 0 for some reason, you can run any command that resets it. I usually use `ver >nul`. – SomethingDark Nov 10 '22 at 06:31
  • ERRORLEVEL is fine. But with `findstr` usage, that doesn't match the pattern causes exit status to non-zero. So you mean that WAR for that is to reset the errorlevel as 0 ? could you please elaborate on `ver >nul`? In my case what would be the solution ? – Kiran Nov 10 '22 at 06:38
  • Sorry, what do you mean by "WAR"? – SomethingDark Nov 10 '22 at 06:39
  • And `findstr` sets errorlevel to 0 when it finds a match and 1 when it doesn't, like you said in your question already. Exit status _IS_ errorlevel. – SomethingDark Nov 10 '22 at 06:41
  • I mean WAR in the sense Work around:) – Kiran Nov 10 '22 at 06:58
  • Workaround is one word so you can't abbreviate it ;) – SomethingDark Nov 10 '22 at 07:05