Skip to main content

How to Get the Exit Code of the Last Command (%ERRORLEVEL%) in Batch Script

The key to writing intelligent and robust batch scripts is the ability to handle errors. When a command runs, it reports its status back to the command processor as a number called an exit code or return code. By checking this code, your script can determine if the command succeeded or failed and then act accordingly. This is the foundation of all error handling and conditional logic in batch scripting.

This guide will teach you how to access the exit code using the special %ERRORLEVEL% variable, explain the critical differences between the classic and modern ways of checking it, and demonstrate powerful shortcuts for conditional command execution.

What is an Exit Code?

An exit code is an integer that a program returns to the operating system when it finishes. There is a universal convention for these codes:

  • 0: Success. The command completed without any errors.
  • Non-zero (e.g., 1, 2, -1073741819): Failure. The command failed for some reason. Different numbers can represent different types of errors.

Your script can check this number to see what happened.

The %ERRORLEVEL% Variable

Windows cmd.exe makes the exit code of the most recently executed external command available through a dynamic, built-in variable named %ERRORLEVEL%. You can see it in action easily.

Example of a script with success and failure:

@ECHO OFF
ECHO Running a command that will succeed...
REM The DIR command on an existing path succeeds.
DIR C:\ > NUL
ECHO The exit code was: %ERRORLEVEL%

ECHO.
ECHO Running a command that will fail...
REM The DIR command on a non-existent path fails.
DIR C:\non_existent_folder > NUL 2> NUL
ECHO The exit code was: %ERRORLEVEL%

Output:

Running a command that will succeed...
The exit code was: 0

Running a command that will fail...
The exit code was: 1

The best practice for checking the exit code is to use a standard IF comparison. This is explicit, clear, and allows for precise checks.

The syntax uses standard comparison operators (EQU for equals, NEQ for not equals, GTR for greater than, etc.).

@ECHO OFF
DIR C:\non_existent_folder > NUL 2> NUL

IF %ERRORLEVEL% NEQ 0 (
ECHO The last command failed!
ECHO Exit code was: %ERRORLEVEL%
) ELSE (
ECHO The last command succeeded.
)
note

This is the recommended method because it is unambiguous. IF %ERRORLEVEL% EQU 0 checks if the code is exactly 0.

The Classic Method: IF ERRORLEVEL N

This is an older syntax that you will see in many scripts. It is important to understand because its behavior is not intuitive.

The command IF ERRORLEVEL N means "If the error level is N or greater".

Example of a script with error:

@ECHO OFF
REM This command returns an exit code of 1.
DIR C:\non_existent_folder > NUL 2> NUL

REM This check is TRUE because 1 is "1 or greater".
IF ERRORLEVEL 1 ECHO The command failed.

REM This check is ALSO TRUE because 1 is "0 or greater"!
IF ERRORLEVEL 0 ECHO The command might have succeeded.

Because IF ERRORLEVEL 0 is always true for any non-negative exit code, it's not a reliable check for success. To check for success with this method, you must check in reverse order:

IF ERRORLEVEL 1 (ECHO Failed) ELSE (ECHO Success)

Due to this confusing "greater than or equal to" behavior, the modern %ERRORLEVEL% EQU 0 syntax is strongly preferred.

Conditional Execution: The && and || Operators

For simple, one-line actions, you can use these powerful operators as a shortcut.

  • && ("AND"): Runs the command on the right only if the command on the left was successful (exit code 0).
  • || ("OR"): Runs the command on the right only if the command on the left failed (non-zero exit code).
@ECHO OFF
REM --- Success Example ---
REM The first command succeeds, so the second one runs.
DIR C:\ > NUL && ECHO DIR command was successful.

ECHO.

REM --- Failure Example ---
REM The first command fails, so the second one runs.
DIR C:\non_existent > NUL 2>NUL || ECHO DIR command failed!

This is an extremely concise and readable way to handle simple error checking.

Common Pitfalls and How to Solve Them

Problem: %ERRORLEVEL% is Fragile and Changes Constantly

The %ERRORLEVEL% variable is updated after every single command finishes. This includes IF, SET, or even ECHO in some cases. If you don't check it immediately, its value will be overwritten.

Solution: Check Immediately or Save the Value

1. Check Immediately (Best Practice):

MyCommand.exe
IF %ERRORLEVEL% NEQ 0 ( ... )

2. Save the Value: If you need to do other things before checking, save the exit code to a normal variable immediately.

MyCommand.exe
SET "LastExitCode=%ERRORLEVEL%"
ECHO The command has finished. Now checking its result...
IF %LastExitCode% NEQ 0 ( ... )

Practical Example: A Reliable File Copy Script

This script uses Robocopy, a tool with well-defined exit codes. It demonstrates a robust way to check for success.

@ECHO OFF
SETLOCAL
SET "SOURCE_FILE=MyData.zip"
SET "DESTINATION=E:\Backups\"

ECHO --- Robocopy with Error Checking ---
robocopy . "%DESTINATION%" "%SOURCE_FILE%"

REM Robocopy exit codes < 8 are considered success.
IF %ERRORLEVEL% LSS 8 (
ECHO [SUCCESS] Robocopy completed without fatal errors.
) ELSE (
ECHO [FAILURE] Robocopy failed with a serious error.
ECHO Exit code was: %ERRORLEVEL%
)

ENDLOCAL

Conclusion

Checking the exit code is the most important skill for moving from simple batch files to powerful, reliable automation scripts.

Key takeaways:

  • An exit code of 0 means success; any non-zero value means failure.
  • The %ERRORLEVEL% variable holds the exit code of the last command.
  • You must check %ERRORLEVEL% immediately, as it is overwritten by the very next command.
  • The modern IF %ERRORLEVEL% EQU 0 syntax is the recommended method for clear and exact checks.
  • Use the && and || operators for concise, single-line conditional execution.

By mastering error handling with %ERRORLEVEL%, you can write scripts that are robust, predictable, and intelligent.