哪些 cmd.exe 内部命令在成功后将 ERRORLEVEL 清除为 0? [英] Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?

查看:36
本文介绍了哪些 cmd.exe 内部命令在成功后将 ERRORLEVEL 清除为 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 批处理脚本中处理错误的一种常用方法是使用诸如
if errorlevel 1 ...if %errorlevel% neq 0 ....很多时候,人们希望错误处理代码保留 ERRORLEVEL.

A frequent method to handling errors within Windows batch scripts is to use things like
if errorlevel 1 ... or if %errorlevel% neq 0 .... Often times one wants the error handling code to preserve the ERRORLEVEL.

我相信所有外部命令都会导致 ERRORLEVEL 被设置为某个值,因此错误处理代码必须在执行外部命令之前将 ERRORLEVEL 保存在环境变量中.

I believe all external commands will always result in ERRORLEVEL being set to some value, so the error handling code must preserve the ERRORLEVEL in an environment variable prior to executing an external command.

但是内部命令呢?问题是,一些内部命令在成功时会将 ERRORLEVEL 清除为 0,而有些则不会.而且我找不到任何指定哪些命令做什么的文档.

But what about internal commands? The problem is, some internal commands clear the ERRORLEVEL to 0 when they succeed, and some do not. And I can't find any documentation specifying which commands do what.

所以问题是,哪些内部命令在成功时将 ERRORLEVEL 清除为 0? 这是关于返回的 ERRORLEVEL 代码的不是一般问题,但严格来说是成功的结果.

So the question is, which internal commands clear the ERRORLEVEL to 0 upon success? This is not a general question about returned ERRORLEVEL codes, but strictly about success results.

有类似 将 ERRORLEVEL 重置为零的最简单方法是什么?Windows 批处理文件:.bat vs .cmd? 给出了部分答案.但我从未见过完整的清单.

There are posts like What is the easiest way to reset ERRORLEVEL to zero? and Windows batch files: .bat vs .cmd? that give partial answers. But I have never seen a comprehensive list.

注意: 多年来我一直对此感到好奇.所以我最终决定进行一系列实验并得出一个明确的答案.我发布此问答是为了分享我的发现.

推荐答案

此答案基于我在 Windows 10 下运行的实验.我怀疑与使用 cmd.exe 的早期 Windows 版本存在差异,但确实如此可能.

另请注意 - 当内部命令遇到错误时,此答案不会尝试记录 ERRORLEVEL 结果(除了关于 DEL 和 ERASE 的一点点)

不仅命令之间存在差异,而且单个命令的行为也会有所不同,具体取决于它是从命令行运行的,还是在带有 .bat 扩展名的批处理脚本中运行的,还是从内部运行的带有 .cmd 扩展名的批处理脚本.

Not only are there difference between commands, but a single command can behave differently depending on whether it was run from the command line, or within a batch script with a .bat extension, or from within a batch script with a .cmd extension.

无论上下文如何,以下命令集永远不会在成功时将 ERRORLEVEL 清除为 0,而是保留先前的 ERRORLEVEL:

The following set of commands never clear the ERRORLEVEL to 0 upon success, regardless of context, but instead preserve the prior ERRORLEVEL:

  • BREAK
  • CLS
  • 回声
  • 本地
  • FOR :显然,DO 子句中的命令可能会设置 ERRORLEVEL,但至少一次迭代的成功 FOR 不会自行将 ERRORLEVEL 设置为 0.
  • 转到
  • IF :显然,IF 执行的命令可能会设置 ERRORLEVEL,但成功的 IF 不会自行将 ERRORLEVEL 设置为 0.
  • 按键
  • 暂停
  • POPD
  • 研发
  • 快速眼动
  • RMDIR
  • SHIFT
  • 开始
  • 标题

无论上下文如何,下一组命令总是在成功时将 ERRORLEVEL 清除为 0:

The next set of commands always clear the ERRORLEVEL to 0 upon success, regardless of context:

  • 光盘
  • CHDIR
  • 颜色
  • 复制
  • 日期
  • DEL:始终清除 ERRORLEVEL,即使 DEL 失败(除非在没有任何文件参数的情况下运行).
  • 目录
  • ERASE :始终清除 ERRORLEVEL,即使 ERASE 失败.(除非在没有任何文件参数的情况下运行).
  • MD
  • MKDIR
  • MKLINK
  • 移动
  • 推送
  • 重命名
  • SETLOCAL
  • 时间
  • 类型
  • VER
  • 验证
  • 音量

然后,如果从命令行发出或在具有 .bat 扩展名的脚本中发出,则这些命令在成功时不会清除 ERRORLEVEL,但如果从脚本发出,则将 ERRORLEVEL 清除为 0带有 .cmd 扩展名.请参阅 https://stackoverflow.com/a/148991/1012053https://groups.google.com/forum/#!msg/microsoft.public.win2000.cmdprompt.admin/XHeUq8oe2wk/LIEViGNmkK0J 了解更多信息.

Then there are these commands that do not clear ERRORLEVEL upon success if issued from the command line or within a script with a .bat extension, but do clear the ERRORLEVEL to 0 if issued from a script with a .cmd extension. See https://stackoverflow.com/a/148991/1012053 and https://groups.google.com/forum/#!msg/microsoft.public.win2000.cmdprompt.admin/XHeUq8oe2wk/LIEViGNmkK0J for more info.

  • 协会会员
  • DPATH
  • FTYPE
  • 路径
  • 提示
  • 设置

最后,有这些命令不适合任何先前的类别:

Lastly, there are these commands that do not fit neatly into any of the prior categories:

  • CALL :如果 :routine 或批处理脚本被调用,则 ERRORLEVEL 仅由被调用的脚本或 :routine 控制.但是,如果 CALLed 命令没有以其他方式设置它,则任何其他类型的对命令的成功调用将始终将 ERRORLEVEL 清除为 0.
    示例:call echo OK.

EXIT :如果在没有 /B 的情况下使用,那么 cmd.exe 会话将终止并且不再有 ERRORLEVEL,只有 cmd.exe 返回码.显然 EXIT/B 0 将 ERRORLEVEL 清除为 0,但是 EXIT/B 没有值会保留之前的 ERRORLEVEL.

EXIT : If used without /B, then the cmd.exe session terminates and there is no more ERRORLEVEL, just the cmd.exe return code. Obviously EXIT /B 0 clears the ERRORLEVEL to 0, but EXIT /B without a value preserves the prior ERRORLEVEL.

我相信所有内部命令都是如此,除非我遗漏了一个未记录的命令.

I believe that accounts for all internal commands, unless there is an undocumented command that I missed.

这篇关于哪些 cmd.exe 内部命令在成功后将 ERRORLEVEL 清除为 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆