Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.)

share|improve this answer
    
thanks, %errorlevel% is not so flexible as I need, but >xxx.log 2>&1 is that I need almost. almost - because log file will be sooooo big... i have so many files in my archive, over 100k... but it is good idea, i`ll trying to use it for brainstorm tomorrow =) can you spell out what is it - 2>&1 I don`t understand it fully? how it work? by the way, i`m use 7z.exe instead 7za, but it does not matter, I see not difference between those programs –  slesar.mira Dec 14 '12 at 1:35
    
>Log.txt 2>&1 will redirect both standard output (=1) and standard error (=2) streams to Log.txt (see here for more). Also, I guess you can use 7z.exe as well, just that I prefer 7za.exe when I'm using it purely from the command line. –  Karan Dec 14 '12 at 1:38
    
I don't think 7-zip writes anything to stderr. –  martineau Dec 14 '12 at 1:41
    
great link, thank You again! =) –  slesar.mira Dec 14 '12 at 1:42
    
@martineau: Maybe not, but that extra 2>&1 will ensure anything directed there is caught. No harm in adding it for sure, even if the app normally doesn't write to stderr (also, why not? IMO it should direct errors to stderr instead of stdout). –  Karan Dec 14 '12 at 1:42
show 1 more comment

7-Zip writes all its messages to stdout, including error and warning messages. This means you can collect all the output from it by redirecting stdout it to a file. This can be done by appending >output_filename to the command line after whatever arguments you're passing to 7-Zip. For example:

7z args... >output_filename

After that, you should be able to parse the output file and look for the lines with "WARNING" or whatever in them. You can do this conditionally based on whether the value of %ERRORLEVEL% in non-zero, which indicates there was some problem, or specifically for it being 1 which means there were non-fatal warnings.

According to the documentation these are the possible exit codes (%ERRORLEVEL% values):

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

share|improve this answer
    
Thank You for your concern! –  slesar.mira Dec 14 '12 at 1:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.