问题描述
ERRORLEVEL是大多数cmd.exe命令根据一系列条件结束更改时返回的值,因此知道命令返回的值是有价值的信息,可能有助于编写更好的批处理文件。所有外部.exe程序在结束时更改ERRORLEVEL(这是和 Win-32 API函数),并且通常这样的值被记录,但内部 cmd.exe命令返回的值在其他地方没有完整记录。
The ERRORLEVEL is a value returned by most cmd.exe commands when they end that change depending on a series of conditions, so knowing the value that the commands return is valuable information that may aid to write better Batch files. All external .exe programs change the ERRORLEVEL when they end (that is an inherent mechanism of both ExitProcess and TerminateProcess Win-32 API functions) and usually such values are documented, but the values returned by internal cmd.exe commands are not fully documented elsewhere.
具有部分ERRORLEVEL值的表格显示在,但只是对于设置ERRORLEVEL = 0一旦成功的内部命令。我建议这样的问题的OP修改它,以便包括不成功的命令返回的值,但他拒绝并邀请我发布自己的问题/答案,所以这里!您必须注意,不等于零的ERRORLEVEL 不必然意味着命令失败!有一些命令以无错误结束并返回大于零的值,以指示不同的退出状态,包括内部命令(如 SET /P
)
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
).
为了更好地利用Batch .bat文件中的内置cmd.exe命令,我们需要知道它们返回的ERRORLEVEL值和此管理中涉及的机制。所以问题是,内部cmd.exe命令将ERRORLEVEL设置为任何值(包括零)?
To make better use of the built-in cmd.exe commands in Batch .bat files we need to know the ERRORLEVEL values they return and the mechanisms involved in this management. So the question is, which internal cmd.exe commands set the ERRORLEVEL to any value (including zero)?
推荐答案
在本回答中,描述了所有内部cmd.exe命令返回的ERRORLEVEL值;它们按值的更改方式分组,并显示为快速参考表。我回顾了其他类似的表,以便组装这一个,但通过在Windows 8.1计算机中执行的测试填补了缺少的值。我尽了最大努力创建这些表格完整和精确,但我没有测试每个人和所有的价值报告在这里,所以它可能是微妙的不一致。
In this answer the ERRORLEVEL values returned by all internal cmd.exe commands are described; they are grouped by the way the value is changed and presented as quick reference tables. I reviewed other similar tables in order to assemble this one, but filled the missing values via tests performed in a Windows 8.1 computer. I made my best effort to create these tables complete and precise, but I had not tested each and everyone of the values reported here, so it may be subtle inconsistencies.
strong>表1 - 不更改先前ERRORLEVEL值的命令
Table 1 - Commands that not change the prior ERRORLEVEL value
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的命令;
Table 3 - Commands that set the ERRORLEVEL on error; otherwise, not change it
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中描述的条件不是时。
The "E" column in Table 3 indicate those commands that change their behavior accordingly to the "Extensions" status as described in the corresponding documentation. When Extensions are enabled (the default) and these commands are placed in a file with .CMD
extension instead of .BAT
one, these commands set SETERRORLEVEL = 0 when they ends with no error, that is, when the conditions described in Table 3 are not present.
表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值:通过 IF ERRORLEVEL / IF%ERRORLEVEL%
命令,或使用命令& ;那么当ERRORLEVEL为0 ||时Cmd elseCmd when ERRORLEVEL不为0
构造。然而,某些特定的命令和重定向错误返回的值仅在第二种情况下有效,并且不能反映在ERRORLEVEL中;我们可以调用退出代码这个值。当此退出代码不为零时,可以将其传递到执行 elseCmd
部分中表1的任何命令的ERRORLEVEL。您可以在上阅读有关此问题的进一步详情。
There are two ways to test the ERRORLEVEL value: via IF ERRORLEVEL / IF %ERRORLEVEL%
command, or using the command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0
construct. However, certain particular commands and redirection errors returns a value that only works in the second case and is not reflected in the ERRORLEVEL; we may call "Exit Code" this value. When this Exit Code is not zero, it can be passed to the ERRORLEVEL executing any command of Table 1 in the elseCmd
part. You may read further details on this matter at this post.
表5 - 设置退出代码的命令或功能
Feature │ Set Exit Code to = when
─────────────┼────────────────────────────────────────────────────────────────────────
command │1 = Command not exist (when ERRORLEVEL = 9009).
redirection │1 = File not exists in "<", path not exists in ">" or ">>".
drive: |1 = Drive unit not exists.
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.
例如,要测试是否发生重定向错误,请使用:
For example, to test if a redirection error happened, use this:
command > C:\Path\that\does\not\exist\file.txt || rem
if errorlevel 1 echo Previous redirection failed
在此示例中, rem
命令用于将退出代码复制到ERRORLEVEL,但是可以使用保留ERRORLEVEL的任何其他内部命令(除了 FOR
和 IF
)。
In this example the rem
command is used to copy the Exit Code to the ERRORLEVEL, but any other internal command that preserve the ERRORLEVEL may be used (excepting FOR
and IF
).
要测试驱动器单元是否存在:
To test if a drive unit exists:
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
这篇关于什么是内部cmd.exe命令设置的ERRORLEVEL值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!