China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-06 01:59
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » ERRORLEVEL to ask? View 1,593 Replies 12
Original Poster Posted 2007-10-04 21:39 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
I want to ask that ERRORLEVEL is the return exit value after a command is executed. How exactly is this explained? What does it mean? For example, the command "call set "c=%%a:00-14-78-42-93-7d=*%%"", does this command have an exit value? What is it?
Floor 2 Posted 2007-10-04 21:42 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
Floor 3 Posted 2007-10-04 21:47 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
```batch
echo off
dir z:
rem 如果退出代码为1(不成功)就跳至标题1处执行
IF ERRORLEVEL 1 goto 1
rem 如果退出代码为0(成功)就跳至标题0处执行
IF ERRORLEVEL 0 goto 0
:0
echo 命令执行成功!
Rem 程序执行完毕跳至标题exit处退出
goto exit
:1
echo 命令执行失败!
Rem 程序执行完毕跳至标题exit处退出
goto exit
:exit
Rem 这里是程序的出口

这段代码执行了dir z:后返回一个什么值呢就是0或1吗那么是不是每条命令执行后都会返回一个退回值呢?
```

If the `dir z:` command finds the specified drive (here, drive Z:), it returns 0; if it doesn't find the drive, it returns 1. And yes, most commands in batch scripting return an exit code (also known as a return value), where 0 typically indicates success and a non-zero value indicates an error or failure.
Floor 4 Posted 2007-10-04 21:48 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
Please ask experts for guidance! Thank you in advance.
Floor 5 Posted 2007-10-04 22:31 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Use %ERRORLEVEL% to check the return value of the previous command.

Usually, a %ERRORLEVEL% value of 0 means the command executed successfully, and if it is greater than or equal to 1, it means the command failed or had an error.

For others, please search the forum, there were many discussions before.
Floor 6 Posted 2007-10-04 22:36 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
Thanks moderator! Why can't I see its return value with set ERRORLEVEL?
Floor 7 Posted 2007-10-04 22:48 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Check with echo %errorlevel%
Floor 8 Posted 2007-10-04 23:00 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
Originally posted by wudixin96 at 2007-10-4 10:48 PM:
Check with echo %errorlevel%

Thank you for your instruction. I know that you can use echo %errorlevel% to view the return value. But I originally thought that errorlevel was a variable and should be able to use set errorlevel to view its value, but practice has proved that this is not the case.
Then I don't understand. Isn't errorlevel a variable?
Floor 9 Posted 2007-10-04 23:03 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
I have read an article saying it's a system variable. Why can something like %systemdrive% be displayed but set errorlevel can't be displayed?
Floor 10 Posted 2007-10-04 23:12 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
Bump
Floor 11 Posted 2007-10-05 00:24 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Re wordexport:

It can be understood like this:
Part of the variables in set can all be called "environment variables". The main function of "environment variables" is to provide some available environment options before the program runs. These are not set after the program runs, but are set in advance, and their values usually do not change.

And errorlevel can be understood as a return value (ExitCode) after a program or command runs. Through this return value, you can roughly judge the running status of the program (whether it is executed normally or an error occurs). Therefore, this value changes with the executed command. Because of these differences from environment variables, so you can't get its value by set errorlevel. As for why exactly, I don't know, Microsoft knows.

Another suggestion is that when judging the return value of a program, it is best to use the format of "if errorlevel ExitCode". Because errorlevel can also be defined as a variable. At this time, if you use %Errorlevel% to judge, it will not be ExitCode, but the value of the variable %errorlevel% you defined. This will cause the judgment to be wrong and make the program process "go astray".
Floor 12 Posted 2007-10-05 09:55 ·  中国 北京 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
Originally posted by wordexport at 2007-10-4 10:36 PM:
Thank you, moderator! Why can't I see the return value when I use set ERRORLEVEL?



SET's help is very clear:

If command extensions are enabled, there are several dynamic environment variables that can be expanded, but they do not appear in the variable list displayed by SET. Each time the variable value is expanded, these variable values are dynamically calculated. If the user defines a variable with any of these names, that definition will replace the dynamic definition described below:

%CD% - Expands to the current directory string.

%DATE% - Expands to the current date in the same format as the DATE command.

%TIME% - Expands to the current time in the same format as the TIME command.

%RANDOM% - Expands to any decimal number between 0 and 32767.

%ERRORLEVEL% - Expands to the current ERRORLEVEL value.

%CMDEXTVERSION% - Expands to the current command processor extension version number.

%CMDCMDLINE% - Expands to the original command line that called the command processor.
Floor 13 Posted 2007-10-05 09:56 ·  中国 浙江 宁波 电信
初级用户
Credits 87
Posts 42
Joined 2007-09-27 19:25
18-year member
UID 98424
Gender Male
Status Offline
Thanks! Learned something new again
Forum Jump: