Exit Codes & errorlevel
Every program that finishes returns an exit code, a number that tells the caller whether it worked. By convention 0 means success and anything else means some kind of failure. CMD stores the exit code of the last command in the dynamic variable %ERRORLEVEL%, and batch files use it to react to errors.
@echo off
> list.txt echo apples
findstr "apples" list.txt >nul
echo after a successful search: %errorlevel%
findstr "pears" list.txt >nul
echo after a failed search: %errorlevel%after a successful search: 0
after a failed search: 1
findstr returns 0 when it finds a match and 1 when it doesn't. Each tool documents its own codes; for example robocopy uses values below 8 for success, and choice returns the number of the key pressed.
Three ways to test it
| Syntax | True when |
|---|---|
if %errorlevel% neq 0 |
the code is not 0 (any comparison operator works) |
if errorlevel 1 |
the code is 1 or higher |
cmd1 || cmd2 |
cmd2 runs only if cmd1 failed |
cmd1 && cmd2 |
cmd2 runs only if cmd1 succeeded |
@echo off
> list.txt echo apples
findstr "kiwi" list.txt >nul
if %errorlevel% neq 0 echo [1] kiwi not found (errorlevel %errorlevel%)
findstr "kiwi" list.txt >nul
if errorlevel 1 echo [2] kiwi not found
findstr "kiwi" list.txt >nul || echo [3] kiwi not found
findstr "apples" list.txt >nul && echo [4] apples found[1] kiwi not found (errorlevel 1)
[2] kiwi not found
[3] kiwi not found
[4] apples found
The old form: if errorlevel N
if errorlevel N is the original syntax from DOS. It means "greater than or equal to N", not "equal to". When you test several values with it, start from the highest:
@echo off
call :exit_with 3
if errorlevel 3 (echo code 3 or more) else if errorlevel 1 (echo code 1 or 2) else (echo success)
call :exit_with 0
if errorlevel 3 (echo code 3 or more) else if errorlevel 1 (echo code 1 or 2) else (echo success)
goto :eof
:exit_with
exit /b %1code 3 or more
success
if errorlevel 0 is therefore always true (for non-negative codes) and a classic bug. It's also safe in blocks: unlike %errorlevel%, it's evaluated when the command runs. Inside a block, use if errorlevel 1 or !errorlevel! with delayed expansion, because %errorlevel% would be expanded too early.
Setting your own exit code: exit /b
exit /b n ends the batch file (or the current subroutine) and sets the exit code to n. Programs that started the script, such as a CI system, a scheduled task or another batch file, read this code to decide whether it worked.
@echo off
call :check_file settings.ini
echo check returned %errorlevel%
> settings.ini echo debug=false
call :check_file settings.ini
echo check returned %errorlevel%
goto :eof
:check_file
if not exist "%~1" (
echo %~1 is missing
exit /b 2
)
echo %~1 is present
exit /b 0 settings.ini is missing
check returned 2
settings.ini is present
check returned 0
| Command | Effect |
|---|---|
exit /b |
end the script or subroutine; the exit code is the current errorlevel |
exit /b 3 |
end it with exit code 3 |
exit |
close the whole CMD process (your window!) |
exit 3 |
close the CMD process with code 3 |
goto :eof |
end the script or subroutine without changing the code |
Inside batch files, use exit /b. A plain exit closes the Command Prompt window of whoever ran the script, which is very annoying when they wanted to read the error message.
Stop on the first error
CMD has no equivalent of Bash's set -e; a failing command doesn't stop the script. The idiomatic pattern is || exit /b after each important step, or a || goto failed to a cleanup section:
@echo off
setlocal
echo step 1: create the build folder
mkdir build || goto failed
echo step 2: write the version file
> build\version.txt echo 1.0 || goto failed
echo step 3: verify the version file
findstr "2.0" build\version.txt >nul || goto failed
echo all steps passed
exit /b 0
:failed
echo build FAILED at the step above
exit /b 1step 1: create the build folder
step 2: write the version file
step 3: verify the version file
build FAILED at the step above
Step 3 fails on purpose (the file contains 1.0, not 2.0), so the script jumps to :failed and returns 1 instead of printing "all steps passed".
Pitfalls
Don't set a variable called ERRORLEVEL. set errorlevel=0 creates a normal variable that hides the dynamic one: from then on %errorlevel% returns your 0 forever, no matter what commands do. To reset the real code, run a command that succeeds, such as ver >nul, or use (call ) (with a space), which sets it to 0. (call) without the space sets it to 1.
Not every command changes errorlevel. Most internal commands set it on failure but leave it untouched on success. echo, rem, set (without /a or /p) and others typically don't reset it to 0. So check the code immediately after the command you care about:
@echo off
> list.txt echo apples
findstr "kiwi" list.txt >nul
echo findstr failed with %errorlevel%
echo this echo does not reset it
echo errorlevel is still %errorlevel%
(call )
echo after (call ): %errorlevel%findstr failed with 1
this echo does not reset it
errorlevel is still 1
after (call ): 0
Expansion inside blocks. %errorlevel% inside a parenthesised block is the value from before the block. Use if errorlevel or !errorlevel!:
@echo off
setlocal enabledelayedexpansion
> data.txt echo apples
if exist data.txt (
findstr "pears" data.txt >nul
echo percent: %errorlevel% delayed: !errorlevel!
)percent: 0 delayed: 1
Reading exit codes of the script itself
From the prompt or from another batch file, check the code a script returned:
C:\Users\ada>deploy.bat
target missing
C:\Users\ada>echo %errorlevel%
1PowerShell reads it as $LASTEXITCODE, and Bash as $?.
Comparison with other shells
| Task | CMD | Bash | PowerShell |
|---|---|---|---|
| last exit code | %errorlevel% |
$? |
$LASTEXITCODE (native programs), $? (success flag) |
| end with code | exit /b 2 |
exit 2 |
exit 2 |
| run on success / failure | &&, || |
&&, || |
&&, || (PowerShell 7) |
| stop on error | || exit /b after each step |
set -e |
$ErrorActionPreference = 'Stop' |
Learn more: Bash Error Handling, PowerShell Error Handling, Batch if.
Test yourself
What does if errorlevel 2 echo X mean?
- Echo X if the exit code is exactly 2
- Echo X if the exit code is 2 or higher
- Echo X if the exit code is less than 2
- Set the exit code to 2
Which command ends a subroutine and returns exit code 5 without closing the window?
- exit 5
- return 5
- exit /b 5
- set errorlevel=5
Why should you never set errorlevel=0?
- It is a syntax error
- It creates a normal variable that hides the real exit code from then on
- It closes CMD
- It resets all variables