批处理文件 - REN 命令的 ErrorLevel 即使失败也会返回 0
这与我之前的问题有关。
ren "C:\Temp\%%A" "%%A"
if errorlevel 0 (
"C:\Program Files\7-Zip\cmdline\7za.exe" a -tzip -mx9 "C:\temp\Zip\%%A.zip" "C:\temp\%%A"
Move "C:\temp\%%A" "C:\Temp\Archive"
)
在上面,即使 REN 命令失败,IF 的计算结果始终为 true。
这个想法是检查文件是否未被任何其他应用程序锁定,如果没有,则将其存档并将其移动到其他地方。
如何最好地做到这一点?
谢谢。
This is related to my earlier question.
ren "C:\Temp\%%A" "%%A"
if errorlevel 0 (
"C:\Program Files\7-Zip\cmdline\7za.exe" a -tzip -mx9 "C:\temp\Zip\%%A.zip" "C:\temp\%%A"
Move "C:\temp\%%A" "C:\Temp\Archive"
)
In the above, the IF evaluates to true always, even if REN command fails.
The idea is to check if a file is not locked by any other application, if not then Archive it and move it elsewhere.
How best to do this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在命令行中键入
help if
以获取有关错误级别处理的一些信息。您的代码的问题在于,对于等于或大于
N
的任何数字,表达式IF ERRORLEVEL N
的计算结果为 true通常只有 ERRORLEVEL 0 表示成功,任何其他 (更大)值是某些错误的标志。要简单地检查是否发生错误,请将检查反转为:
或者作为替代方法,退出脚本:
Type
help if
on the command line to get some information on the errorlevel handling.The problem with your code is, that the expression
IF ERRORLEVEL N
is evaluated to true for any number equal to or greater thanN
Usually only ERRORLEVEL 0 indicates success, any other (greater) value is a sign of some error. To simply check, if nor error occurred, reverse your check to:
or as an alternative, exit the script:
您还可以制作一个 rem.bat ,它将使错误级别调用类似 IF ERRORLEVEL==300 调用 rem.bat 或者您可以使用级别 0 使每个错误级别可解锁。它只会使应用程序运行更流畅,但不会造成风扇速度保持不变的滞后,因为错误级别将使用更多的 CPU 使用率。
you can also make a rem.bat that'll make the error level calling something like this IF ERRORLEVEL==300 call rem.bat or you can just make every error level unlockable by using a level of 0. you can varrie on things not only will it make the app run more smoothy but itll creat no lagging in the way ur fan speed will stay the same as the errorlevel will use more cpu usage.
REN 是一个内部命令,不会设置 ERRORLEVEL (我正在寻找相同的答案 此处)
REN is an internal command and does not set ERRORLEVEL (I'm looking for the same answer here)