批次:“rd”的退出代码错误也是0 [英] batch: Exit code for "rd" is 0 on error as well

查看:187
本文介绍了批次:“rd”的退出代码错误也是0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个批处理(.bat)脚本,我需要处理一个文件夹的删除失败的情况。我使用%errorlevel%来捕获退出代码,但是在 rd 命令的情况下,似乎没有工作:

  C:\Users\edo\Desktop> rd testdir 
目录不为空

C:\Users\edo\Desktop> echo%errorlevel%
0

为什么?你建议什么?

谢谢!

解决方案

哇,这是我见过的第二个例子其中ERRORLEVEL未正确设置!请参阅 Windows中的文件重定向和%errorlevel%



解决方案与检测重定向故障相同。

  rd testdir || echo命令失败! 

奇怪的是,当您使用 || 运算符,如果文件夹不为空,则ERRORLEVEL被正确设置为145,如果文件夹不存在,则将其设置为2。所以你甚至不需要做任何事情。您可以有条件地执行一条评论,然后错误级别将被正确设置。

  rd testdir || rem 
echo%errorlevel%

更新2016-01-21



回到2015年4月,Andreas Vergison在一则评论中声称, || 没有设置ERRORLEVEL 拒绝访问或...正在使用...错误。我当时有Windows 7,而且我不认为我已经验证了他的说法,只是假设他是正确的。但是我最近在Windows 10中进行了测试,并且 || 总是将ERRORLEVEL设置为非零的错误。请注意,在运行每个命令之前,(call)是一种将ERRORLEVEL强制为0的奥秘方式。另请注意,我的cmd.exe会话已启用延迟扩展。

  C:\test>(call)& rd垃圾&& echo OK || echo ERROR!errorlevel! 
访问被拒绝。
错误5

C:\test>(call)&测试&& echo OK || echo ERROR!errorlevel!
目录不为空。
错误145

C:\test>(call)& rd \test&&& echo OK || echo ERROR!errorlevel!
进程无法访问该文件,因为该进程被另一个进程使用。
错误32

C:\test>(call)& rd notExists&& echo OK || echo ERROR!errorlevel!
系统找不到指定的文件。
错误2


I'm writing a batch (.bat) script and I need to handle the case in which the deletion of a folder fails. I'm using %errorlevel% to catch the exit code, but in the case of the rd command it seems not to work:

C:\Users\edo\Desktop>rd testdir
Directory is not empty

C:\Users\edo\Desktop>echo %errorlevel%
0

Why? What do you suggest?
Thanks!

解决方案

Wow, this is the 2nd case I've seen where ERRORLEVEL is not set properly! See File redirection in Windows and %errorlevel%.

The solution is the same as for detecting redirection failure. Use the || operator to take action upon failure.

rd testdir || echo The command failed!

The bizarre thing is, when you use the || operator, the ERRORLEVEL is then set properly to 145 if the folder was not empty, or 2 if the folder did not exist. So you don't even need to do anything. You could conditionally "execute" a remark, and the errorlevel will then be set properly.

rd testdir || rem
echo %errorlevel%

Update 2016-01-21

Back in April of 2015, Andreas Vergison claimed in a comment that || did not set the ERRORLEVEL for "Access Denied", or "...In Use..." errors. I had Windows 7 at the time, and I don't think I verified his claim, but just assumed he was correct. But I have recently tested in Windows 10, and the || always sets the ERRORLEVEL to non-zero upon error. Note that (call ) is an arcane way of forcing the ERRORLEVEL to 0 before I run each command. Also note that my cmd.exe session has delayed expansion enabled.

C:\test>(call ) & rd junk && echo OK || echo ERROR !errorlevel!
Access is denied.
ERROR 5

C:\test>(call ) & rd test && echo OK || echo ERROR !errorlevel!
The directory is not empty.
ERROR 145

C:\test>(call ) & rd \test && echo OK || echo ERROR !errorlevel!
The process cannot access the file because it is being used by another process.
ERROR 32

C:\test>(call ) & rd notExists && echo OK || echo ERROR !errorlevel!
The system cannot find the file specified.
ERROR 2

这篇关于批次:“rd”的退出代码错误也是0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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