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 have a batch file that loops through the contents of a directory and compresses the files in the directory as follows;

for %%a in (c:\data\*.*) do if "%%~xa" == "" "C:\Program Files\7-Zip\7za.exe" a -tzip -mx9 "%%a.zip" "%%a"

Seeing that I am using 7zip to compress the file, it returns the message "everything is okay" if it has successfully compressed the file and it then moves onto the next file in any.

What I would like to do is the following;

  1. Only move to the next file if the response is "everything is okay"
  2. If the response is anything but "everything is okay", the error is logged
  3. Since an error has occurred, it attempts to compress the file again
  4. Once when it has succeeded i.e. "everything is okay" it goes to the next file
  5. Steps 3 & 4 only occur a maximum of 3 times before it gives up and moves onto the next file.

How can I achieve this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted
  • (1) and (2)

    You can use a combination of labels, if, and returns codes:

    for %%a in (…) do if … "7za.exe" … "%%a" & if not %errorlevel%==0 (set z_file="%%a" & goto error)
    …
    :error
      echo Something was borked for "%z_file%"! >> Compress.log
    
  • (3)

    You can call the compression line from the error routine:

    :error
      echo Error…
      7z…
    
  • (4) and (5)

    Now you’re getting complicated and beyond the limitations of a batch-file. It is possible, but it requires advanced functions and hacks. You can put the compression and error routines in subroutines and call them as appropriate. You can also use set to make a counter (e.g., set /a counter=counter+1) (this is one reason to enable command extensions) and if for logic (e.g., if %counter% leq 3 call :compress)

share|improve this answer
    
Why do you have errorlevel defined as a variable e.g. %errorlevel%?. Why would 4 be complex? I would have thought that assuming the first file failed on try 1 it would attempt to compress the file again and if it passed it would return to the loop? –  PeanutsMonkey Aug 30 '12 at 7:17
    
Errorlevel is a built-in variable. It’s not the number of retries that’s complex, it’s the concept itself; batch-files were never meant for advanced functional programming or logic branches like this. Like I said, it can be done for the most part, but it won’t be pretty and will require using many of the advanced features of the command-interpreter (including using command-extensions). –  Synetech Aug 30 '12 at 13:48
    
using the if not %errorlevel%==0 (set z_file="%%a" & goto error) style, you could make extra error messages, using goto try2, goto lasttry and use that to count the number of retries. using call batchlabel may give the ability to return to where you left off –  Sean Cheshire Aug 30 '12 at 14:25
    
Yup, you could do it that way too if you don’t want too many retries (it would result in code-duplication, but it might be a little simpler to write than using a counter). –  Synetech Aug 30 '12 at 14:33
    
@Synetech - What is the & symbol used for? The examples I have seen seem to use && –  PeanutsMonkey Aug 30 '12 at 19:55

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.