我有一个批处理文件,其中我执行以下命令来列出归档文件的内容:
"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"
压缩文件已被故意损坏。
cmd.exe 显示以下内容:
我该如何在我的代码中捕获这个错误?
我有一个批处理文件,其中我执行以下命令来列出归档文件的内容:
"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"
压缩文件已被故意损坏。
cmd.exe 显示以下内容:
我该如何在我的代码中捕获这个错误?
%ERRORLEVEL%
变量中。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,因此您应该按降序放置它们。
在调用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