How to write error status for command line 7-zip in variable (or, instead, in text file)

7-zipbatchcommand line

I use 7-zip (in my batch files) to pack some directory and send archive by email. When I use some file in this directory at the same time the batch file is running (I start the batch files with Task Scheduler), I see warning messages in process. After this, when the archive is ready, 7-zip displays a message like this: "WARNING: Cannot open 29 files" before sending the email. When this happens, I want to be able to set an environment variable (something like %MESSAGE%) with the value "29 warnings in progress" and to put this message in the subject of the email. But all that can do right now is use the %ERRORLEVEL% variable. If I can't set a %MESSAGE% variable, is it possible to write warning messages to a file, then parse this file to extract the last line?

Best Answer

I suggest you use the 7-Zip Command Line Version (7za.exe).

A command like the following will redirect all output (including from stderr) to Log.txt:

7za a Test.7z *.* >Log.txt 2>&1

Additionally, 7-Zip returns the following exit codes which you can use in your batch file with %ERRORLEVEL%:

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.


Edit: If you don't want such a verbose log containing all those Compressing <filename> lines, use this command instead:

7za a Test.7z *.* | findstr /i /v "pavlov scanning compressing" >Log.txt 2>&1

(The findstr command with switches above excludes all lines containing the words within quotes.)