内部cmd.exe命令设置了哪些ERRORLEVEL值?

31
ERRORLEVEL是大多数cmd.exe命令结束时返回的一个值,该值会根据一系列条件而变化,因此了解命令返回的值是有价值的信息,可以帮助编写更好的批处理文件。所有外部的.exe程序在结束时都会改变ERRORLEVEL(这是ExitProcessTerminateProcess Win-32 API函数的固有机制),通常这些值都有文档记录,但内部cmd.exe命令返回的值并没有完全记录在其他地方。请注意保留HTML标签。
A table with partial ERRORLEVEL values appears at this question, but just for internal commands that set ERRORLEVEL=0 "upon success". I suggested the OP of such question to modify it in order to also include the values returned by "not successful commands", but he refused and invited me to post my own question/answer, so here it is! You must note that an ERRORLEVEL different than zero does not necessarily means that the command failed! There are some commands that end with no error and return a value greater than zero to indicate different "exit status", including internal commands (like SET /P).
为了更好地利用批处理.bat文件中内置的cmd.exe命令,我们需要了解它们返回的ERRORLEVEL值以及涉及此管理的机制。因此,问题是,哪些内部cmd.exe命令将ERRORLEVEL设置为任何值(包括零)?
1个回答

55
在这个答案中,介绍了所有内部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是微软建议使用的命令,用于将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中 不会 反映;我们可以称其为“退出码”。当此退出码不为零时,它可以传递给表1中任何命令执行的 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

我敢肯定在我检查时它不在那里。也许你进行了多次快速编辑。有太多要测试的东西,我怀疑在这个答案完成之前可能需要很长时间。理想情况下,应该使用无效值测试每个命令的每个选项,包括带和不带 || 的选项。我觉得有些命令产生非1的非零值,但大多数都产生1。我怀疑其他命令可能会产生除0或1以外的结果。当然,还有其他未列出的条件,至少可以导致ERRORLEVEL 1。 - dbenham
@dbenham: 嗯,这些表格确实很有用,即使它们并不包括所有现有命令的所有可能错误情况(顺便说一句,一个简单的检查管理员权限的小例子:date %date%)。无论如何,新的数据可以在发现时添加,但我真的怀疑任何“正常”的批处理文件用户是否需要一个尚未列出的值... - Aacini
3
EXITEXIT number 不会返回 ERRORLEVEL,而是返回进程的退出代码。如果终止 cmd 会话本身是由父级 cmd.exe 会话启动的,则父会话将像处理任何外部命令一样处理退出代码 - 它将把退出代码值传递给父级的 ERRORLEVEL。 - dbenham
1
由于错误的开关是不同类型的语法错误,这里是考虑到错误开关的内部命令摘要 - http://pastebin.com/2t2QsjRB - npocmaka
当传递不存在的网络路径时,rddelcdpushd 命令将设置错误级别为1。可能调用了一些外部函数。 - npocmaka
显示剩余21条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接