Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file test error level

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?

like image 419
Daniel Powell Avatar asked Jul 25 '11 06:07

Daniel Powell


People also ask

What is error level 1 in batch file?

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.

What is error level in batch file?

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 )

What does Error Level 1 mean?

Errorlevel is 0 if the process was successful. Errorlevel is 1 or greater if the process encountered an error. Testing errorlevel.

What is error level CMD?

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.


1 Answers

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 redirection
like image 179
Anders Avatar answered Oct 09 '22 16:10

Anders