批处理文件 - REN 命令的 ErrorLevel 即使失败也会返回 0

发布于 2024-08-28 04:19:59 字数 380 浏览 6 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠栀 2024-09-04 04:20:01

在命令行中键入 help if 以获取有关错误级别处理的一些信息。

您的代码的问题在于,对于等于或大于 N 的任何数字,表达式 IF ERRORLEVEL N 的计算结果为 true

通常只有 ERRORLEVEL 0 表示成功,任何其他 (更大)值是某些错误的标志。要简单地检查是否发生错误,请将检查反转为:

IF NOT ERRORLEVEL 1 (
   REM your code here
)

或者作为替代方法,退出脚本:

IF ERRORLEVEL 1 EXIT /B

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 than N

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:

IF NOT ERRORLEVEL 1 (
   REM your code here
)

or as an alternative, exit the script:

IF ERRORLEVEL 1 EXIT /B
怂人 2024-09-04 04:20:01

您还可以制作一个 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.

冬天的雪花 2024-09-04 04:20:01

REN 是一个内部命令,不会设置 ERRORLEVEL (我正在寻找相同的答案 此处

REN is an internal command and does not set ERRORLEVEL (I'm looking for the same answer here)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文