How to check the exit code of the last command in batch file?

71

9

Inside a batch file on Windows, I use 7-zip like this:

...\right_path\7z a output_file_name.zip file_to_be_compressed

How could I check the exit code of 7z and take the appropriate action ?

Misha Moroshko

Posted 2010-10-01T04:47:03.857

Reputation: 5 573

1

Also asked on Stackoverflow: How do I get the application exit code from a Windows command line?

– Deanna – 2013-06-24T11:42:56.590

Answers

83

Test for a return code greater than or equal to 1:

if ERRORLEVEL 1 echo Error

or

if %ERRORLEVEL% GEQ 1 echo Error

or test for a return code equal to 0:

if %ERRORLEVEL% EQU 0 echo OK

You can use other commands such as GOTO where I show echo.

Paused until further notice.

Posted 2010-10-01T04:47:03.857

Reputation: 86 075

2Found cases where %ERRORLEVEL% is 0 even though an error occurred. Happened when checking %ERRORLEVEL% in a cmd file. Trying start /wait didn't work. The only thing that worked is if errorlevel 1 (...) – AlikElzin-kilaka – 2015-04-13T12:59:28.423

4

Be aware, errorlevel is not an environment variable. Here's a good summary of the pitfalls and subtleties.

– Nick Westgate – 2015-06-17T06:18:32.310

Might I suggest using NEQ instead of EQU to support detecting negative return codes? No idea if Windows XP does that, but it’s a thing on modern Windows… – binki – 2016-07-25T17:16:01.033

I tried your code. I got the following error: 0 was unexpected this time. – Misha Moroshko – 2010-10-01T05:13:41.703

2@Misha: You may have tried it with the percent signs the way I originally posted it. Try it without them or try the other versions I added. – Paused until further notice. – 2010-10-01T05:24:07.580

@binki, negative codes are indicating system errors, not application ones. Which is rarely seen in the wild. – AnrDaemon – 2018-07-05T11:30:20.940

@AnrDaemon Don’t you want your script to correctly fail in case of a system error instead of silently ignoring it though? I only commented that because I was having trouble with commands failing and not being detected because they were returning negative numbers and hoped that the answer could be fixed to avoid allowing others to fall into the trap of using GEQ or IF ERRORLEVEL instead of a pattern like SET ERR=0 IF NOT ERRORLEVEL 0 SET ERR=1 IF ERRORLEVEL 1 SET ERR=1. – binki – 2018-07-05T15:05:10.340

Depends on the script. Normally, I only handle specific error codes, and just rethrow everything else. This is not the right place to discuss this. – AnrDaemon – 2018-07-05T21:23:28.120

I disagree, it's the perfect place. The question is how to check the exit code of the last command. if the exit code is -1 then we should know about it. Regardless of whether that is an application error code or a system error code, it's still an error code and it should be trapped and handled. I want to know about all errors, so NEQ 0 wins for me every time over GEQ 1... – Geoff Griswald – 2019-12-31T16:10:09.673

10

This really works when you have: App1.exe calls -> .bat which runs --> app2.exe

App2 returns errorlevel 1... but you need to catch that in the .bat and re-raise it to app1... otherwise .bat eats the errorlevel and app1 never knows.

Method:

In .bat:

app2.exe
if %ERRORLEVEL% GEQ 1 EXIT /B 1

This is a check after app2 for errorlevel. If > 0, then the .bat exits and sets errorlevel to 1 for the calling app1.

Jonesome Reinstate Monica

Posted 2010-10-01T04:47:03.857

Reputation: 1 680

4it could be even better if you returned the same error back to app1. i didn't try this out, but it should work : if %ERRORLEVEL% GEQ 1 EXIT /B %ERRORLEVEL%. – Viktor Fonic – 2014-07-18T11:24:33.403

3At least in Windows, %ERRORLEVEL% can be a negative number (e.g. I have a program that returns -1 on errors). IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL% might be a better option. But you really need to know what the program returns on errors. Some programs return certain non-zero codes for special types of success. – Euro Micelli – 2014-11-13T19:23:13.717

If app2 is the last thing you run in the bat file, the error code will propagate. – AnrDaemon – 2018-07-05T11:31:16.337