2011-10-13 28 views
8

Tôi có một tập tin thực thi, trong đó tôi thực hiện các dòng sau vào danh sách các nội dung của một kho lưu trữ:Catch một lỗi bên trong một tập tin thực thi (7-zip)

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

Kho lưu trữ là cố ý hỏng.

màn cmd.exe này:

enter image description here

Làm thế nào tôi có thể bắt lỗi này trong mã của tôi?

Trả lời

19

Mã thoát của bất kỳ chương trình nào được lưu trữ trong biến số %ERRORLEVEL% trong tập lệnh lô.

Từ hướng dẫn 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 

Vì vậy: bạn có thể làm:

"\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 

Thận trọng, if errorlevel N kiểm tra rằng %ERRORLEVEL% là lớn hơn hoặc bằng so với N, do đó bạn nên đặt chúng trong Thứ tự giảm dần.

+0

Thanx. Điều đó hoạt động hoàn hảo! –

4

Kiểm tra xem ERRORLEVEL được đặt thành 1 ngay sau khi gọi đến 7z.exe và phản ứng một cách thích hợp. ERRORLEVEL là mã thoát khỏi chương trình cuối cùng được chạy. Mã thoát của 1 hoặc nhiều hơn cho biết lỗi trong khi 0 cho biết thành công. Lệnh IF ERRORLEVEL kiểm tra xem lối ra lớn hơn hoặc bằng đối số sao cho IF ERRORLEVEL kiểm tra mức lỗi của một hoặc nhiều.

Dưới đây là một ví dụ:

"\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 
Các vấn đề liên quan