Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 内部cmd.exe命令设置的ERRORLEVEL值是什么?_Batch File_Cmd_Errorlevel - Fatal编程技术网

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

Batch file 内部cmd.exe命令设置的ERRORLEVEL值是什么?,batch-file,cmd,errorlevel,Batch File,Cmd,Errorlevel,ERRORLEVEL是大多数cmd.exe命令在结束时返回的值,该值根据一系列条件而变化,因此了解命令返回的值是有价值的信息,有助于编写更好的批处理文件。所有外部.exe程序结束时都会更改错误级别(这是和Win-32 API函数的固有机制),通常会记录这些值,但内部cmd.exe命令返回的值在其他地方没有完整记录 带有部分ERRORLEVEL值的表出现在,但仅适用于“成功时”将ERRORLEVEL设置为0的内部命令。我建议对该问题的OP进行修改,以便也包括“not successful comm

ERRORLEVEL是大多数cmd.exe命令在结束时返回的值,该值根据一系列条件而变化,因此了解命令返回的值是有价值的信息,有助于编写更好的批处理文件。所有外部.exe程序结束时都会更改错误级别(这是和Win-32 API函数的固有机制),通常会记录这些值,但内部cmd.exe命令返回的值在其他地方没有完整记录

带有部分ERRORLEVEL值的表出现在,但仅适用于“成功时”将ERRORLEVEL设置为0的内部命令。我建议对该问题的OP进行修改,以便也包括“not successful commands”返回的值,但他拒绝了,并邀请我发布我自己的问题/答案,就是这样!您必须注意,ERRORLEVEL不同于零并不一定意味着命令失败!有些命令结束时没有错误,返回的值大于零,表示不同的“退出状态”,包括内部命令(如
SET/P


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

在这个答案中,描述了所有内部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
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.
表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.
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-设置错误级别的命令;否则,请勿更改它

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.
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
的文件中时,当这些命令在没有错误的情况下结束时,即当表3中描述的条件不存在时,会将SETERRORLEVEL设置为0

表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.
退出代码管理

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.
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.
有两种方法可以测试ERRORLEVEL值:通过
IF ERRORLEVEL/IF%ERRORLEVEL%
命令,或者当ERRORLEVEL为0时使用
命令和&thenCmd;当ERRORLEVEL不为0时使用elseCmd。但是,某些特定命令和重定向错误返回的值仅在第二种情况下有效,不会反映在ERRORLEVEL中;我们可以将此值称为“退出代码”。当此退出代码不为零时,可以将其传递给执行elseCmd部分中表1中任何命令的ERRORLEVEL。您可以在网站上阅读有关此问题的更多详细信息

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

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
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.
在本例中,
rem
命令用于将退出代码复制到ERRORLEVEL,但可以使用保留ERRORLEVEL的任何其他内部命令(除了
FOR
IF

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

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以外的结果。当然,还有其他未列出的条件至少会导致错误级别1。@dbenham:嗯,这些表肯定很有用,即使它们不包括所有现有命令的所有可能错误情况(顺便说一句,这是一个检查管理员权限的简单方法:
date%date%
)。无论如何,新数据可能会在发现时添加,但我真的怀疑任何普通批处理文件用户是否需要此处未列出的值…@jeb:我真的不想包括需要额外解释的非标准机制,而且绝大多数用户肯定不会使用这些机制。我在表3中添加了
goto labelNotExist
EXIT
EXIT number
不返回错误级别,而是返回流程退出代码。如果终止cmd会话本身由父cmd.exe会话启动,然后,父会话将像处理任何外部命令一样处理退出代码-它将把退出代码值传输到父会话的ERRORLEVEL。由于坏开关是不同类型的语法错误,下面是考虑坏开关的内部命令的恢复-