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

GG.*_*GG. 7 windows error-handling runtime-error 7zip batch-file

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

"\Program Files\7-Zip\7z.exe" l "\Backup Google Docs.7z"
Run Code Online (Sandbox Code Playgroud)

存档故意损坏.

cmd.exe显示如下:

在此输入图像描述

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

Ben*_*oit 20

任何程序的退出代码都存储在%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 
Run Code Online (Sandbox Code Playgroud)

所以:你可以这样做:

"\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
Run Code Online (Sandbox Code Playgroud)

注意,if errorlevel N检查%ERRORLEVEL%大于或等于N,因此您应该按降序排列.