Batch File – How to Check the Exit Code of the Last Command in Batch File

batchwindows xp

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 ?

Best Answer

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.

Related Question