SoFunction
Updated on 2025-03-10

Question about the %errorlevel% variable related to command status code in bat script

%errorlevel% is commonly used in bat scripts to express the return value of the previous command, that is, the command execution status code, also known as the command exit code

Generally, there are only two values ​​returned by the execution result of the previous command, 0 and non-0 (such as the common 1, 2, 4, 5, 9009, etc.). 0 will generally be considered successful, non-0 is a failure or an exception.

For 9009, a command error or a command that does not exist often occurs in cmd. At this time, a status code such as 9009 will be returned.

1. For the %errorlevel% variable, it is generally used more with if, as follows:

dir 
if %errorlevel% ==0 (del /q/f )

2. When writing a bat script yourself, you can define the exit status code of the %errorlevel% command through the exit command, as follows:

echo "test var errorlevel"
exit /b 1

Note: Exit requires the /b option, and /b is to specify that you want to exit the current batch script instead of . If executed outside a batch script, it will exit

Note: After ext /b 1 is executed, the current batch script will be exited, and even if there is content behind it, it will not continue to execute!

However, when I test some commands, there will also be exceptions for this variable (the reason is unknown at present)

C:\Users\5201351\Desktop>echo 123
123
C:\Users\5201351\Desktop>echo %errorlevel%
0
C:\Users\5201351\Desktop>abcdefg
'abcdefg' is not an internal or external command, nor a runnable program
or batch file.
C:\Users\5201351\Desktop>echo %errorlevel%
9009
C:\Users\5201351\Desktop>echo 123456
123456
C:\Users\5201351\Desktop>echo %errorlevel%
9009
C:\Users\5201351\Desktop>

The following are supplements

errorlevel and %errorlevel% are both the return value of the previous command.

if errorlevel value cmmand sentence pattern, meaning: if the returned error code value is greater than or equal to the value, the cmmand operation will be performed;

if %errorlevel%==value cmmand sentence pattern, meaning: if the returned error code value is equal to the value, the cmmand operation will be performed.

Generally speaking, there are only two values ​​returned by the execution result of a command. "Success" is represented by 0 and "Failed" is represented by 1, but in fact, the return value of errorlevel can be between 0 and 255.

For example, there are 5 default errorlevel values ​​in xcopy, representing 5 execution statuses:

0The file was copied successfully

1 The copy file was not found

2 The user terminated the xcopy operation through CTRL C.

4 An initialization error occurred

5 A disk write error occurred

If %ERRORLEVEL% logarithmic values ​​are not limited to equality, and can be controlled using parameters. It can also achieve the effect of the previous sentence

For example: IF %ERRORLEVEL% LEQ 1 goto OK

All comparison parameters are as follows:

EQU - equal to

NEQ - Not equal to

LSS - Small

LEQ - less than or equal to

GTR - Greater than

GEQ - greater than or equal to

I'll give you another example

dir d:\ | find "jb51\net"
if %errorlevel%==0 goto ok
if %errorlevel%==1 goto end
:ok
rd d:\jb51\net
:end
md d:\jb51\net

Determine whether there is a jb51\net directory on the d disk. If it does not exist, create it. If it does, delete the content in the folder. However, it cannot be used. For details, please refer to this article.

https:///article/

This is all about this article about the %errorlevel% variable related to the command status code in the bat script. For more related content of the %errorlevel% variable in the bat script, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!