I want something like this:- if %ERRORLEVEL% GEQ 1
&& %ERRORLEVEL% neq 255 GOTO Not closed by user
. But this syntax is not working.
2 Answers
What about this? XP or higher required, of course...
if %errorlevel% GEQ 1 (
if %errorlevel% NEQ 255 goto :NotClosed
::other statements go here, if you need 'em
::don't forget to close the parenthesis on the last statement ->)
To clarify further:
if %errorlevel% EQU 0 (
::commands you want to perform if 0 go here. Notice ->)
else (
::We got here because errorlevel was GEQ 1 or less than one...watch out
::if you program returns negative errorlevels!
if %errorlevel% NEQ 255 goto :NotClosed
::other statements go here. But don't forget the parenthesis ->)
-
-
Yes. But perform the check before the NEQ check. So, do the
if %errorlevel% EQU 0
first, then the NEQ 255.– JSanchezJan 16, 2014 at 18:06
For completeness, here's an answer for JP Software's TCC/LE (a command interpreter that can also run such scripts), which supports the syntax from JSanchez's answer, for downwards compatibility, but also has two better alternatives of its own.
The alternatives are …
… its own multi-line iff
command
iff %ERRORLEVEL% GE 1 then
iff %ERRORLEVEL% NE 255 then
goto :NotClosedByUser
endiff
endiff
With iff
one doesn't have the caveats about variable expansion happening "too soon" that one has with parenthesized compound commands.
… compound expressions with logical operators
if %ERRORLEVEL% GE 1 .and. %ERRORLEVEL% NE 255 goto NotClosedByUser
This has the advantage of being almost identical to what you wanted in the question. TCC even supports GEQ
and NEQ
for compatibility. Yes, it doesn't require the colon in the goto
command.
Further reading
- JP Software. IFF. Take Command / TCC Help.
- JP Software. Conditional Expressions. Take Command / TCC Help.