gpt4 book ai didi

batch-file - 内部 cmd.exe 命令设置的 ERRORLEVEL 值是多少?

转载 作者:行者123 更新时间:2023-12-03 08:01:01 27 4
gpt4 key购买 nike

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

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

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

最佳答案

在此答案中,描述了所有内部 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.

表 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 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%命令,或使用 command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0构造。但是,某些特定命令和重定向错误返回的值仅适用于第二种情况,而不会反射(reflect)在 ERRORLEVEL 中;我们可以将这个值称为“退出代码”。当此退出代码不为零时,可以将其传递给执行 elseCmd 中表 1 中的任何命令的 ERRORLEVEL。部分。您可以在 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 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

关于batch-file - 内部 cmd.exe 命令设置的 ERRORLEVEL 值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34987885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com