![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-06 00:59 |
48,039 topics / 350,124 posts / today 2 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » ERRORLEVEL to ask? |
| Printable Version 1,592 / 12 |
| Floor1 wordexport | Posted 2007-10-04 21:39 |
| 初级用户 Posts 42 Credits 87 | |
|
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?
|
|
| Floor2 wordexport | Posted 2007-10-04 21:42 |
| 初级用户 Posts 42 Credits 87 | |
| Floor3 wordexport | Posted 2007-10-04 21:47 |
| 初级用户 Posts 42 Credits 87 | |
|
```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. |
|
| Floor4 wordexport | Posted 2007-10-04 21:48 |
| 初级用户 Posts 42 Credits 87 | |
|
Please ask experts for guidance! Thank you in advance.
|
|
| Floor5 lxmxn | Posted 2007-10-04 22:31 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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. |
|
| Floor6 wordexport | Posted 2007-10-04 22:36 |
| 初级用户 Posts 42 Credits 87 | |
|
Thanks moderator! Why can't I see its return value with set ERRORLEVEL?
|
|
| Floor7 wudixin96 | Posted 2007-10-04 22:48 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
Check with echo %errorlevel%
|
|
| Floor8 wordexport | Posted 2007-10-04 23:00 |
| 初级用户 Posts 42 Credits 87 | |
Originally posted by wudixin96 at 2007-10-4 10:48 PM: 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? |
|
| Floor9 wordexport | Posted 2007-10-04 23:03 |
| 初级用户 Posts 42 Credits 87 | |
|
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?
|
|
| Floor10 wordexport | Posted 2007-10-04 23:12 |
| 初级用户 Posts 42 Credits 87 | |
|
Bump
|
|
| Floor11 lxmxn | Posted 2007-10-05 00:24 |
| 版主 Posts 4,938 Credits 11,386 | |
|
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". |
|
| Floor12 knoppix7 | Posted 2007-10-05 09:55 |
| 银牌会员 Posts 634 Credits 1,287 From cmd.exe | |
Originally posted by wordexport at 2007-10-4 10:36 PM: 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. |
|
| Floor13 wordexport | Posted 2007-10-05 09:56 |
| 初级用户 Posts 42 Credits 87 | |
|
Thanks! Learned something new again
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |