本文介绍了什么是内部CMD.EXE命令设置ERRORLEVEL值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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值的表出现在this问题,但只是为设置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).

为了更好地利用蝙蝠,我们需要知道它们返回的ERRORLEVEL值文件批量内置CMD.EXE命令和参与这一管理机制。所以,问题是,它的内部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值;它们由值被改变的方式,psented作为快速参照表$ P $分组。我查阅了其他类似的表格以组装这一块,而是通过在Windows 8.1的电脑进行测试,填补了缺失值。我做了创建这些表完整和precise我最大的努力,但我没有测试过每一个人在这里报告的值,所以它可能会有细微的不一致。

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.

表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 - 设置该错误级别上的错误命令;否则,不改变它

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中描述的条件的的present

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.

退出code管理

有两种方法来测试ERRORLEVEL值:通过 IF ERRORLEVEL / IF%ERRORLEVEL%命令,或使用命令和放大器;&安培; thenCmd当ERRORLEVEL为0 || elseCmd当ERRORLEVEL不为0 构造。但是,某些具体的命令和重定向错误返回一个值,该值仅工作在第二种情况下,是不的反映在错误级别;我们可以称之为退出code这个值。当这种退出code不为零,它可以传递到 elseCmd 部分执行表1中的任何命令的ERRORLEVEL。你可以在this帖子。

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 - 命令或设置退出code的特征

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 ">>".
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 命令用于退出code拷贝到错误级别,但preserve任何其他内部命令的错误级别可以是使用(除了如果)。更多的例子:

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). More examples:

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值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:25