batch file test error level

Viewed 103124

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?

3 Answers

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.

Related