99

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
  • 7,423
  • 9
  • 26
  • 31
  • 2
    Also asked on Stackoverflow: [How do I get the application exit code from a Windows command line?](http://stackoverflow.com/q/334879/588306) – Deanna Jun 24 '13 at 11:42

4 Answers4

115

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

if ERRORLEVEL 1 echo Error

or

if %ERRORLEVEL% NEQ 0 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.

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • I tried your code. I got the following error: `0 was unexpected this time.` – Misha Moroshko Oct 01 '10 at 05:13
  • 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. – Dennis Williamson Oct 01 '10 at 05:24
  • 2
    Found 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 Apr 13 '15 at 12:59
  • 5
    Be aware, errorlevel is [not an environment variable](http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx). Here's a good summary of the [pitfalls and subtleties](http://stackoverflow.com/questions/10935693/foolproof-way-to-check-for-nonzero-error-return-code-in-windows-batch-file). – Nick Westgate Jun 17 '15 at 06:18
  • 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 Jul 25 '16 at 17:16
  • @binki, negative codes are indicating system errors, not application ones. Which is rarely seen in the wild. – AnrDaemon Jul 05 '18 at 11:30
  • @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 Jul 05 '18 at 15:05
  • 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 Jul 05 '18 at 21:23
  • 1
    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 Dec 31 '19 at 16:10
  • I've changed my answer to use `NEQ 0`. – Dennis Williamson Jun 29 '22 at 15:32
13

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.

  • 6
    it 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 Jul 18 '14 at 11:24
  • 3
    At 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 Nov 13 '14 at 19:23
  • If app2 is the last thing you run in the bat file, the error code will propagate. – AnrDaemon Jul 05 '18 at 11:31
4

I had a batch script in Teamcity pipeline and it did not exit after it's child script did exit with code 1.

To fix the problem I added this string IF %ERRORLEVEL% NEQ 0 EXIT 1 after the child script call.

main-script.bat

...some code
call child-script.bat
IF %ERRORLEVEL% NEQ 0 EXIT 1
...some code

After the child script call exit result is saved to %ERRORLEVEL%. If it did exit with an error %ERRORLEVEL% would not be equal to 0 and in this case we exit with code 1 (error).

1

Try this:

cd.>nul | call Your_7z_File.Bat && Goto :Next || Goto :Error

:: Or...

cd.>nul | ...\right_path\7z.exe a output_file_name.zip file_to_be_compressed && Goto :Next || Goto :Error

1) Set errorlevel == 0

cd.>nul

2) Redirect by | to simultaneously call Your_7z_File.Bat

cd.>nul | call Your_7z_File.Bat

3) Lets the output/errorlevel to a conditional execution && and || filter the next action

cd.>nul | call Your_7z_File.Bat && Goto :Next || Goto :Error

You can also test it in command line adding echo/:

cd.>nul | call Your_7z_File.Bat && echo\Goto :Next || echo\Goto :Error
Io-oI
  • 7,588
  • 3
  • 12
  • 41