Get error code from batch file

I have a batch file that executes several executable files, and I want it to complete successfully, but stop if the exit code is <> 0. How to do this?

+36
windows batch-file exit-code
Aug 10 '10 at 18:12
source share
2 answers

It looks like you need the "If Errorlevel" command. Assuming your executable returns an exit code other than 0, you do something like:

myProgram.exe if errorlevel 1 goto somethingbad echo Success! exit :somethingbad echo Something Bad Happened. 

The error level check is performed as a check of greater or equal, therefore any value other than 0 will cause a jump. Therefore, if you need to check more than one specific output value, you first need to check the highest.

+43
Aug 10 '10 at 18:20
source share

You can also use conditional processing characters to perform a simple success / failure check. For example:

 myProgram.exe && echo Done! 

will print Done! only if myProgram.exe returns with error level 0.

 myProgram.exe || PAUSE 

will pause the batch file if myProgram.exe returns a non-zero error level.

+28
Aug 11 2018-10-10 at
source share



All Articles