3

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 2

3

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 ->)
2
  • How to check whether it is equal to 0? if %ERRORLEVEL%==0 works?
    – Deb
    Jan 16, 2014 at 17:55
  • Yes. But perform the check before the NEQ check. So, do the if %errorlevel% EQU 0 first, then the NEQ 255.
    – JSanchez
    Jan 16, 2014 at 18:06
1

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

1
  • Ya, The second one is what I was asking for. Thanks.
    – Deb
    Jan 18, 2014 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .