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 ?
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 ?
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.
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.
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).
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