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 ?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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.
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.
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.
...some code call child-script.bat IF %ERRORLEVEL% NEQ 0 EXIT 1 ...some code
After the child script call, exit result is save to
%ERRORLEVEL%. If it did exit with error the value would be not equal to 0. So we check this and if it is the case we exit with code 1 (error).
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 ==
2) Redirect by
| to simultaneously call
cd.>nul | call Your_7z_File.Bat
3) Lets the output/errorlevel to a conditional execution
|| filter the next action
cd.>nul | call Your_7z_File.Bat && Goto :Next || Goto :Error
You can also test it in command line adding
cd.>nul | call Your_7z_File.Bat && echo\Goto :Next || echo\Goto :Error