A frequent method to handling errors within Windows batch scripts is to use things likeif errorlevel 1 ...
or if %errorlevel% neq 0 ...
. Often times one wants the error handling code to preserve the ERRORLEVEL.
I believe all external commands will always result in ERRORLEVEL being set to some value, so the error handling code must preserve the ERRORLEVEL in an environment variable prior to executing an external command.
But what about internal commands? The problem is, some internal commands clear the ERRORLEVEL to 0 when they succeed, and some do not. And I can't find any documentation specifying which commands do what.
So the question is, which internal commands clear the ERRORLEVEL to 0 upon success? This is not a general question about returned ERRORLEVEL codes, but strictly about success results.
There are posts like What is the easiest way to reset ERRORLEVEL to zero? and Windows batch files: .bat vs .cmd? that give partial answers. But I have never seen a comprehensive list.
Note: I've been curious about this for years. So I finally decided to run a bunch of experiments and come up with a definitive answer. I'm posting this Q&A to share what I have found.
Set or Force an exit code To force an ERRORLEVEL of 1 to be set without exiting, run a small but invalid command like COLOR 00 or run (CALL) which does nothing other than set the ERRORLEVEL to 1. To clear the ERRORLEVEL back to 0, run (call ), which does nothing except set the ERRORLEVEL to 0.
It is common to use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file. EXIT /B at the end of the batch file will stop execution of a batch file.
You can also add the cls command in any batch file to clear screen. If you are trying to hide any confidential information or previously entered commands from the command, the F7 (for doskey) can still show previously entered commands.
Updated: 06/06/2021 by Computer Hope. In Microsoft Windows and MS-DOS, an errorlevel is the integer number returned by a child process when it terminates. Errorlevel is 0 if the process was successful. Errorlevel is 1 or greater if the process encountered an error.
This answer is based on experiments I ran under Windows 10. I doubt there are differences with earlier Windows versions that use cmd.exe, but it is possible.
Also note - This answer does not attempt to document the ERRORLEVEL result when an internal command encounters an error (except for a wee bit concerning DEL and ERASE)
Not only are there difference between commands, but a single command can behave differently depending on whether it was run from the command line, or within a batch script with a .bat
extension, or from within a batch script with a .cmd
extension.
The following set of commands never clear the ERRORLEVEL to 0 upon success, regardless of context, but instead preserve the prior ERRORLEVEL:
The next set of commands always clear the ERRORLEVEL to 0 upon success, regardless of context:
Then there are these commands that do not clear ERRORLEVEL upon success if issued from the command line or within a script with a .bat
extension, but do clear the ERRORLEVEL to 0 if issued from a script with a .cmd
extension. See https://stackoverflow.com/a/148991/1012053 and https://groups.google.com/forum/#!msg/microsoft.public.win2000.cmdprompt.admin/XHeUq8oe2wk/LIEViGNmkK0J for more info.
Lastly, there are these commands that do not fit neatly into any of the prior categories:
CALL : If a :routine or batch script is CALLed, then ERRORLEVEL is exclusively controlled by the CALLed script or :routine. But any other type of successful CALL to a command will always clear ERRORLEVEL to 0 if the CALLed command does not otherwise set it.
Example: call echo OK
.
EXIT : If used without /B
, then the cmd.exe session terminates and there is no more ERRORLEVEL, just the cmd.exe return code. Obviously EXIT /B 0
clears the ERRORLEVEL to 0, but EXIT /B
without a value preserves the prior ERRORLEVEL.
I believe that accounts for all internal commands, unless there is an undocumented command that I missed.
Your description of CALL
command is incomplete:
CALL : Clears ERRORLEVEL if the CALLed command does not otherwise set it. Example:
call echo OK
.
Check this small example:
@echo off call :setTwo echo Set two: %errorlevel% call :preserve echo Preserve: %errorlevel% call echo Reset echo Reset: %errorlevel% call :subNotExists 2> NUL echo Sub not exist: %errorlevel% goto :EOF :setTwo exit /B 2 :preserve echo Preserve exit /B
Output:
Set two: 2 Preserve Preserve: 2 Reset Reset: 0 Sub not exist: 1
CALL
description should say something like this:
call echo OK
, but if the called command is a subroutine it preserves the prior ERRORLEVEL. If the called subroutine does not exist, it sets the ERRORLEVEL to 1.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With