为了账号安全,请及时绑定邮箱和手机立即绑定

内部cmd.exe命令设置的ERRORLEVEL值是什么?

内部cmd.exe命令设置的ERRORLEVEL值是什么?

繁星coding 2019-07-13 15:11:41
内部cmd.exe命令设置的ERRORLEVEL值是什么?ERRORLEVEL是大多数cmd.exe命令根据一系列条件结束更改时返回的值,因此知道命令返回的值是有价值的信息,可以帮助编写更好的批处理文件。所有外部.exe程序在结束时都会更改ERRORLEVEL(这是这两种程序的固有机制退出过程和终末过程-32 api函数),通常这些值都有文档记录,但返回的值由内部cmd.exe命令在其他地方没有完整的文档记录。具有部分ERRORLEVEL值的表出现在这个问题,但仅用于设置ERRORLEVEL=0的内部命令。我建议这样的问题的OP修改它,以便也包括“不成功的命令”返回的值,但他拒绝了,并邀请我张贴我自己的问题/答案,所以,就这样!您必须注意,与零不同的ERRORLEVEL确实是不这意味着命令失败了!有些命令以无错误结尾,并返回大于零的值,以指示不同的“退出状态”,包括内部命令(如SET /P).为了更好地利用批处理.bat文件中内置的cmd.exe命令,我们需要知道它们返回的ERRORLEVEL值以及该管理所涉及的机制。所以问题是,内部cmd.exe命令将ERRORLEVEL设置为任何价值(包括零)?
查看完整描述

3 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

在这个答案中,描述了由所有内部cmd.exe命令返回的ERRORLEVEL值;它们根据值的更改方式进行分组,并显示为快速引用表。为了组装这个表,我检查了其他类似的表,但是通过在Windows8.1计算机上执行的测试来填充缺失的值。我尽了最大的努力来完整和精确地创建这些表,但是我没有测试这里报告的每个和每个值,所以这可能是微妙的不一致。

表1-不更改先前ERRORLEVEL值的命令

BREAK
ECHO
ENDLOCAL
FOR      Not change the ERRORLEVEL by itself. See "Exit Code" below.
IF       Not change the ERRORLEVEL by itself.
PAUSE
RD       Not change the ERRORLEVEL on errors, but the "Exit Code". See below.
REM
RMDIR    Same as RD.
SET      Plain SET command (no arguments). See "Table 3" below.
TITLE

表2-根据结果将ERRORLEVEL设置为0或1的命令

Command │ Set ERRORLEVEL = 0 when       │ Set ERRORLEVEL = 1 when
────────┼───────────────────────────────┼──────────────────
CD      │Current directory was changed. │Directory not exists or is not accessible.
CHDIR   │Same as CD.                    │
COLOR   │Color was changed.             │Background and foreground colors are the same.
COPY    │File(s) was processed.         │File not found or bad parameters given.
DATE    │Date was changed or not given. │User has no admin privileges.
DEL     │Almost always, excepting when: │Bad or no parameters given.
DIR     │Same as COPY.                  │
ERASE   │Same as DEL.                   │
MD      │Directory was created.         │Directory could not be created.
MKDIR   │Same as MD.                    │
MKLINK  │Link was created.              │Link could not be created or bad parameters given.
MOVE    │File(s) was moved/renamed.     │File not found, could not be moved/renamed or bad parameters.
PUSHD   │Same as CD.                    │+ Bad switch given.
REN     │Same as MOVE.                  │
RENAME  │Same as MOVE.                  │
SETLOCAL│New environment was created.   │Bad parameters given.
TIME    │Time was changed or not given. │User has no admin privileges.
TYPE    │Same as COPY.                  │
VERIFY  │Right or no parameters given.  │Bad parameters given.
VOL     │Volume label was displayed.    │Drive not found or bad parameters given.

表3-将ERRORLEVEL设置为错误的命令;否则,不更改

Command      │E│ Set ERRORLEVEL to = when
─────────────┼─┼─────────────────────────────────────────
ASSOC        │*│1 = Extension associations could not be changed.
CLS          │ │1 = Bad switch given.
DPATH        │*│1 = Data path could not be established.
FTYPE        │*│1 = File type associations could not be changed.
GOTO label   │ │1 = Label not exist *in a subroutine* (equivalent to: EXIT /B 1).
KEYS         │ │1 = Bad switch given.
PATH         │*│1 = Path could not be changed.
POPD         │ │1 = Bad switch given.
PROMPT       |*│1 = Prompt could not be changed.
SET var      │*│1 = No variable with such name exists.
SET var=value│*│1 = Variable name start with "/" not enclosed in quotes.
SET /P       │*│1 = Read an empty line or at end of file.
SET /A       │*│1073750988 = Unbalanced parentheses, 1073750989 = Missing operand, 
             │ │1073750990 = Syntax error, 1073750991 = Invalid number,
             │ │1073750992 = Number larger than 32-bits, 1073750993 = Division by zero.
SHIFT        │ │1 = Bad switch given.

表3中的“E”列表示那些相应地将它们的行为更改为“扩展”状态的命令,如相应的文档中所描述的那样。当启用扩展名(默认)并将这些命令放置在以下文件中时.CMD扩展而不是.BAT第一,这些命令在结束时设置SETERRORLEVEL=0,即当表3中描述的条件是现在时。

表4-特殊情况

CALL Table1     │If the called command is anyone of Table 1 (excepting FOR and IF): set ERRORLEVEL = 0.
CALL subroutine │If the subroutine is called, not change prior ERRORLEVEL value;
                │otherwise (subroutine not exists): set ERRORLEVEL = 1.
EXIT /B, EXIT   │Not change prior ERRORLEVEL value.
EXIT /B number  │Set ERRORLEVEL to given number.
EXIT number     │Ends cmd.exe and set its returning ERRORLEVEL value to given number.
START command   │If command is started, not change ERRORLEVEL; otherwise, set ERRORLEVEL = 9059.
START /WAIT bat |When the started Batch file end, set ERRORLEVEL = value from 'EXIT number' commmand.
notExist        │If a non-existent command is entered for execution, set ERRORLEVEL = 9009.
VER             │Set ERRORLEVEL = 0 almost always. If /? parameter is given, not change ERRORLEVEL.

出口代码管理

有两种方法可以测试ERRORLEVEL值:VIAIF ERRORLEVEL / IF %ERRORLEVEL%命令,或使用command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0构造。但是,某些特定命令和重定向错误返回的值仅适用于第二种情况,反映在ERRORLEVEL中;我们可以将“退出代码”称为这个值。如果退出代码不是零,则可以将其传递给执行表1的任何命令的ERRORLEVEL。elseCmd部分。有关此事的详情,请参阅这个职位.

表5-设置退出代码的命令或功能

Feature      │ Set Exit Code to = when
─────────────┼───────────────────────────────────────────────
command      │1 = Command not exist (when ERRORLEVEL = 9009).
redirection  │1 = File not exists in "<", path not exists or access denied in ">" ">>".
drive:       |1 = Drive unit not exists.
POPD         |1 = No matching PUSHD was previously executed.
RD           │1 = Bad switch given, 2 = Directory not found, 5 = Access denied,
             │32 = Directory in use, 145 = Directory not empty.
FOR /F       │1 = No data was processed.

例如,要测试是否发生重定向错误,请使用以下命令:

command > C:\Path\that\does\not\exist\file.txt || rem
if errorlevel 1 echo Previous redirection failed

在本例中,rem命令用于将退出代码复制到ERRORLEVEL,但可以使用保留ERRORLEVEL的任何其他内部命令(例外)FORIF).

若要测试驱动器单元是否存在,请执行以下操作:

U: || rem
if errorlevel 1 echo Previous set current drive to U: unit failed

更多的例子:

rd c:\Some\directory 2> NUL || rem
if %errorlevel% equ 0 (
   echo Directory deleted
) else if %errorlevel% equ 2 (
   echo Directory not found
) else if %errorlevel% equ 5 (
   echo Can not access the directory, check rights
) else if %errorlevel% equ 32 (
   echo Can not delete current directory
) else if %errorlevel% equ 145 (
   echo Directory is not empty, use /S switch
)


(for /F "options" %%a in (input.txt) do echo %%a) || rem
if errorlevel 1 echo Previous FOR didn't processed any value


查看完整回答
反对 回复 2019-07-13
?
千万里不及你

TA贡献1784条经验 获得超9个赞

我可以发誓当我检查的时候它不在那里。也许你有过多次快速的继承编辑。有这么多的东西需要测试,我怀疑这个答案可能需要很长时间才能完成。理想情况下,每个命令的每个选项都应该使用无效的值进行测试,不管是有还是没有。||..我觉得有些命令产生1以外的非零值是很奇怪的,但是大多数命令会产生1。我怀疑额外的命令可能会产生0或1以外的东西。当然,还有一些额外的未列出的条件至少可以导致ERRORLEVEL 1。

查看完整回答
反对 回复 2019-07-13
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

这些表确实很有用,即使它们不包括所有现有命令的所有可能的错误情况(BTW是一个小示例,这是检查管理权限的简单方法:date %date%)。无论如何,新的数据可能会随着发现而增加,但我真的很怀疑正常批处理文件用户可能需要一个未在此列出的值.

查看完整回答
反对 回复 2019-07-13
  • 3 回答
  • 0 关注
  • 1184 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信