问题描述
一个常见的方法,Windows批处理脚本中处理错误是使用之类的东西结果如果错误级别1 ...
或如果%ERRORLEVEL%NEQ 0 ...
。很多时候,一个人想的错误处理code至preserve的ERRORLEVEL。
A frequent method to handling errors within Windows batch scripts is to use things likeif errorlevel 1 ...
or if %errorlevel% neq 0 ...
. Often times one wants the error handling code to preserve the ERRORLEVEL.
我相信所有的外部命令总是会导致ERRORLEVEL被设置为某个值,所以错误处理code前必须执行一个外部命令preserve在环境变量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.
但对于内部命令?问题是,一些内部命令清除错误级别为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.
所以现在的问题是,它的内部命令在成功清除错误级别为0?这就是的不的一般约返回ERRORLEVEL $问题C $ CS,但严格对成功结果。
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.
有像最简单的方法和的,让部分答案。但是,我从来没有见过一个COM prehensive列表。
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.
注:的 我一直好奇这个多年。所以,我终于决定运行一堆实验,并拿出一个明确的答案。我张贴这种Q&功放; A分享我发现的
推荐答案
的这个答案是基于我的实验的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.
以下命令集从未在成功清除错误级别为0,而不管上下文,而是preserve事先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
- ECHO
- ENDLOCAL
- FOR:显然,溶解氧子句中的命令可以设定错误级别,但成功的用于与至少一个迭代不ERRORLEVEL设置成0自身 。
- GOTO
- 如果:显然,如果执行的命令可能会设置错误级别,但成功的,如果不自行设置错误级别为0
- 键
- 暂停
- POPD
- RD
- REM
- RMDIR
- SHIFT
- 开始
- 标题
下一组命令总是在成功清除错误级别为0,无论背景:
The next set of commands always clear the ERRORLEVEL to 0 upon success, regardless of context:
- CD
- CHDIR
- 颜色
- 复制
- 日期
- DEL:总是清除ERRORLEVEL,即使DEL失败的(当没有任何文件参数运行时除外)的 。
- DIR
- ERASE:总是清除ERRORLEVEL,即使ERASE失败。的(当没有任何文件参数运行时除外)的
- MD
- MKDIR
- MKLINK
- 将
- PUSHD
- REN
- 重命名
- SETLOCAL
- 时间
- 键入
- VER
- 验证
- VOL
然后还有这些命令,如果命令行或以的.bat
扩展名的脚本中发出了做成功后不明确的错误级别,但做清除ERRORLEVEL到0,如果从脚本发出了的.cmd
扩展。请参见和https://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 http://stackoverflow.com/a/148991/1012053 and https://groups.google.com/forum/#!msg/microsoft.public.win2000.cmdprompt.admin/XHeUq8oe2wk/LIEViGNmkK0J for more info.
- ASSOC
- DPATH
- FTYPE
- PATH
- 提示
- 设置
最后,有这些命令不适合整齐到任何现有的类别:
Lastly, there are these commands that do not fit neatly into any of the prior categories:
-
CALL:如果一个:常规:常规或批处理脚本被调用,那么ERRORLEVEL完全由被调用的脚本或控制。但到命令的任何其他类型的成功调用将始终明确ERRORLEVEL为0,如果调用的命令不会以其他方式对其进行设置。结果
例如:调用回声确定
退出:如果没有使用/ B
,那么CMD.EXE会话终止,没有更多的ERRORLEVEL,只是CMD.EXE回报code。显然 EXIT / B 0
清除错误级别为0,但 EXIT / B
没有价值preserves事先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的内部命令在成功清除错误级别为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!