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

11 浏览
0 Comments

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

当大部分cmd.exe命令结束时,它们会根据一系列条件返回改变的值,因此知道命令返回的值是有用的信息,可以帮助编写更好的批处理文件。所有外部.exe程序在结束时都会改变ERRORLEVEL(这是ExitProcessTerminateProcess Win-32 API函数的固有机制),并且通常这些值是有文档记录的,但内部cmd.exe命令返回的值并未在其他地方完全记录。

一个部分ERRORLEVEL值的表格出现在这个问题中,但只适用于将ERRORLEVEL设置为0“成功”时的内部命令。我建议问题的OP修改它,以便包括“不成功的命令”返回的值,但他拒绝了并邀请我发表自己的问题/答案,所以这里就是!您必须注意,ERRORLEVEL与零不同并不一定意味着命令失败!有些命令在不出错的情况下结束并返回大于零的值以表示不同的“退出状态”,包括内部命令(如SET / P)。

为了更好地使用批处理文件中内置的cmd.exe命令,我们需要知道它们返回的ERRORLEVEL值以及涉及此管理的机制。因此,问题是,哪些内部cmd.exe命令将ERRORLEVEL设置为任何值(包括零)?

admin 更改状态以发布 2023年5月21日
0
0 Comments

在这个答案中,描述了所有内部cmd.exe命令返回的ERRORLEVEL值;它们按照值的改变方式进行分组,并以快速参考表的形式呈现。我审核了其他类似的表格,以组装这个表,但是通过在Windows 8.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.

VERIFY是MS建议用于将ERRORLEVEL设置为0或1的命令;可以参见SETLOCAL /?中的示例。

表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 ends, 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值:通过IF ERRORLEVEL / IF %ERRORLEVEL%命令,或使用command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0结构。然而,某些特定的命令和重定向错误返回一个仅在第二种情况下有效且不反映在ERRORLEVEL中的值;我们可以称此值为“退出代码”。当此退出代码不为零时,它可以在elseCmd部分中执行任何表1中的命令以将其传递给ERRORLEVEL。您可以在此帖子中阅读更多关于此问题的详细信息。

表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

0