Get error code from within a batch file

Viewed 55818

I have a batch file that runs a couple executables, and I want it to exit on success, but stop if the exit code <> 0. How do I do this?

3 Answers

A solution better than Hellion's answer is checking the %ERRORLEVEL% environment variable:

IF %ERRORLEVEL% NEQ 0 (
  REM do something here to address the error
)

It executes the IF body, if the return code is anything other than zero, not just values greater than zero.

The command IF ERRORLEVEL 1 ... misses the negative return values. Some progrmas may also use negative values to indicate error.

BTW, I love Cheran's answer (using && and || operators), and recommend that to all.

For more information about the topic, read this article

Related