28

I'm trying to conditionally run an exe from a batch file conditionally upon another exe executing successfully.

I've tried a few different combinations of IF and ERRORLEVEL but none seem to work

"..\..\..\TeamBuildTypes\Current Branch\DatabaseUpdate.exe" -s localhost\sql2008r2 

IF %ERRORLEVEL% 1(
"..\..\..\TeamBuildTypes\Current Branch\DatabaseUpdate.exe" -s localhost\sql2008
)
Pause

Gives me the error

1( was unexpected at this time.

Where am I going wrong here?

Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
  • Hmm, perhaps `1(` is wrong? Have you tried `1 (` with a space? And by the way use either `if errorlevel 1 (...` or `if %errorlevel%==1 (...` – jeb Jul 25 '11 at 06:44
  • @jeb: That seems to be the actual cause of the error, I mean, the mixed up syntax in the OP's script. – Andriy M Jul 25 '11 at 22:49
  • the problem is that %ERRORLEVEL% is getting substituted. Use if errorlevel, not if %ERRORLEVEL% – Steve Carter Aug 23 '18 at 10:40

3 Answers3

58

IF ERRORLEVEL ... is a special syntax supported since the DOS days, the %ERRORLEVEL% variable support was added in WinNT.

The original syntax is used like this:

call someapp.exe
if errorlevel 1 goto handleerror1orhigher
echo succuess... 

To use the variable, use the normal IF syntax: if %errorlevel%==0 echo success...

Note that %errorlevel% stops working if someone does set errorlevel=foo and it might not get updated for internal cmd.exe commands.

An alternative solution is to use &&:

call someapp.exe && (echo success) || (echo error!)

There are (at least) two known cases where errorlevel is broken and you must use || instead:

Anders
  • 97,548
  • 12
  • 110
  • 164
  • 4
    I've only used either `&&` or `||`, never both of them on one line. Didn't know that together they essentially work like `IF...ELSE`. That's a nice thing to learn, thanks! – Andriy M Jul 25 '11 at 22:46
  • 5
    if errorlevel 0 means "if errorlevel is greater than or equal zero", that is, any value. The right way to ask for success is: if not errorlevel 1 echo success... – Aacini Jul 26 '11 at 01:31
  • @Aacini: well yes, but I'm checking 1 before 0, that is a valid way to do it. – Anders Jul 26 '11 at 14:04
  • 3
    @Anders: well yes, but in this case the entire "if errorlevel 0" part is unnecessary and may confuse the novice. – Aacini Jul 27 '11 at 09:44
  • Interesting, you could combine this with call :successLabel and call :failLabel if you are checking this in a for loop (in normal flow you can combine with goto) – George Birbilis Apr 12 '14 at 12:59
9

Negative errorlevels can create problem. Try something like this:

IF '%ERRORLEVEL%'=='0' GOTO OK
demongolem
  • 9,474
  • 36
  • 90
  • 105
cdndcnj
  • 99
  • 1
  • 1
  • 1
    The syntax is not correct for use in a batch file. It should be: IF ERRORLEVEL 0 GOTO OK – SPlatten Aug 22 '18 at 06:33
  • @SPlatten Checking after invoking svn, "IF ERRORLEVEL 0 GOTO OK" does not work. Checking "IF (%ERRORLEVEL%) == (0) GOTO OK" does work. – R.J. Dunnill Apr 05 '21 at 20:42
  • The key is surrounding the environment variable with %%, this means get the contents of the variable. – SPlatten Apr 06 '21 at 06:15
1

Never use parenthesis to enclose any variables. Parentheses are a block construct that will break DO and IF blocks and other things. Instead use safe plain characters like "X" to enclose variables E.g. To test for ERRORLEVEL of ONLY 0 use ... IF X%ERRORLEVEL%X == X0X Echo or GOTO etc ... It is safer this way as many special characters have special meanings and will break more complex code without any warning and take hours to debug. I know, this trap has befallen me whenever I forget my own advice.

XOR42
  • 11
  • 1