捕获批处理文件 (7-zip) 中的错误

发布于 2024-12-09 18:21:42 字数 249 浏览 1 评论 0原文

我有一个批处理文件,在其中执行以下行来列出存档的内容:

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"

存档被故意损坏。

cmd.exe 显示以下内容:

在此处输入图像描述

如何在代码中捕获此错误?

I have a batch file in which I execute the following line to list the contents of an archive:

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"

The archive is intentionally corrupted.

cmd.exe displays this:

enter image description here

How can I catch this error in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ゃ懵逼小萝莉 2024-12-16 18:21:42

任何程序的退出代码都存储在批处理脚本中的 %ERRORLEVEL% 变量中。

来自 7-zip 手册:

7-Zip returns the following exit codes:

Code Meaning 
0 No error 
1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed. 
2 Fatal error 
7 Command line error 
8 Not enough memory for operation 
255 User stopped the process 

所以:你可以这样做:

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"
if errorlevel 255 goto:user_stopped_the_process
if errorlevel 8 goto:not_enough_memory
if errorlevel 7 goto:command_line_error
if errorlevel 2 goto:fatal_error
if errorlevel 1 goto:ok_warnings

注意,if errorlevel N 检查 %ERRORLEVEL% 大于或等于 N,因此你应该将它们放入降序排列。

Any program's exit code is stored in the %ERRORLEVEL% variable in a batch script.

From the 7-zip manual:

7-Zip returns the following exit codes:

Code Meaning 
0 No error 
1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed. 
2 Fatal error 
7 Command line error 
8 Not enough memory for operation 
255 User stopped the process 

So: you can do:

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"
if errorlevel 255 goto:user_stopped_the_process
if errorlevel 8 goto:not_enough_memory
if errorlevel 7 goto:command_line_error
if errorlevel 2 goto:fatal_error
if errorlevel 1 goto:ok_warnings

Caution, if errorlevel N checks that %ERRORLEVEL% is greater or equal than N, therefore you should put them in descending order.

○闲身 2024-12-16 18:21:42

在调用 7z.exe 后检查 ERRORLEVEL 是否设置为 1,并做出适当反应。 ERRORLEVEL 是上次运行的程序的退出代码。退出代码为 1 或更大表示错误,而 0 表示成功。 IF ERRORLEVEL 命令检查退出是否大于或等于参数,因此 IF ERRORLEVEL 检查错误级别为 1 或更高。

这是一个例子:

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z" > nul
IF ERRORLEVEL 1 goto ziperror
@echo 7-Zip worked
goto :eof

:ziperror
@echo 7-Zip failed
goto :eof

Check if the ERRORLEVEL is set to 1 just after the call to 7z.exe and react appropriately. The ERRORLEVEL is the exit code from the last program that was run. An exit code of 1 or more indicates an error while zero indicates success. The IF ERRORLEVEL command checks if the exit is greater than or equal to the argument so IF ERRORLEVEL checks for an error level of one or more.

Here is an example:

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z" > nul
IF ERRORLEVEL 1 goto ziperror
@echo 7-Zip worked
goto :eof

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