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?
1. Incorrect function. Indicates that Action has attempted to execute non-recognized command in Windows command prompt cmd.exe. 2. The system cannot find the file specified.
Batch file error level:%ERRORLEVEL% is an environment variable that contains the last error level or return code in the batch file – that is, the last error code of the last command executed. Error levels may be checked by using the %ERRORLEVEL% variable as follows: IF %ERRORLEVEL% NEQ 0 ( DO_Something )
Errorlevel is 0 if the process was successful. Errorlevel is 1 or greater if the process encountered an error. Testing errorlevel.
The ERRORLEVEL is a value returned by most cmd.exe commands when they end that change depending on a series of conditions, so knowing the value that the commands return is valuable information that may aid to write better Batch files.
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:
RD
/RMDir
>
file redirectionIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With