How to Pause Command Prompt on 7-Zip Errors

When running automated 7-Zip tasks via Windows Command Prompt or a batch script, execution often completes and closes the window immediately, hiding crucial error messages. This article explains how to detect 7-Zip failure codes using the %ERRORLEVEL% variable and the || conditional operator so you can halt the script with the pause command whenever an archive process fails.


Method 1: Using the Inline Conditional Operator (||)

The simplest way to pause execution on failure is using the Windows batch || (OR) operator. In Command Prompt, commands chained with || only execute if the preceding command returns a non-zero exit code.

7z a "backup.zip" "C:\SourceFolder" || pause

If 7-Zip creates the archive successfully, it exits with code 0, and the script moves to the next line. If an error occurs (such as missing files or invalid syntax), the command returns a non-zero exit code and immediately triggers pause, keeping the window open until a key is pressed.


Method 2: Checking %ERRORLEVEL% Explicitly

For multi-step scripts, checking the %ERRORLEVEL% system variable gives you greater control over logging, error messages, and conditional execution.

@echo off
"C:\Program Files\7-Zip\7z.exe" x "archive.zip" -o"C:\Destination"

if %ERRORLEVEL% NEQ 0 (
    echo.
    echo An error occurred during extraction. Error Code: %ERRORLEVEL%
    pause
    exit /b %ERRORLEVEL%
)

echo Extraction completed successfully.

%ERRORLEVEL% NEQ 0 checks whether the return code is anything other than standard success (0). If an error is detected, the script prints an alert, halts via pause, and exits cleanly.


Understanding 7-Zip Exit Codes

7-Zip provides specific exit codes to help you distinguish between warnings and fatal errors:

  • 0: Normal execution (no errors)
  • 1: Warning (non-fatal error, such as some files being locked or skipped)
  • 2: Fatal error
  • 7: Command-line parameter error
  • 8: Not enough memory for the operation
  • 255: Process was interrupted by the user

If you only want to pause on fatal errors while ignoring minor warnings, adjust the conditional check:

"C:\Program Files\7-Zip\7z.exe" a "archive.zip" "C:\Data"

if %ERRORLEVEL% GEQ 2 (
    echo Fatal error encountered.
    pause
)

By adding these checks to your batch routines, you ensure command windows stay open long enough to inspect and troubleshoot any 7-Zip operation failures.