Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"

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
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)
      echo Something was borked for "%z_file%"! >> Compress.log
  • (3)

    You can call the compression line from the error routine:

      echo Error…
  • (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 – SeanC 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

Expanding on Synetech's answer – this is a working script:

@echo off
set max_try=3

for %%I in (c:\data\*.*) do if "%%~xI" == "" (
    set cnt_try=0
    call :7zip "%%~I"
exit /B %ERL%

set /A cnt_try+=1
"C:\Program Files\7-Zip\7za.exe" a -tzip -mx9 "" "%~1"
if %ERL% NEQ 0 if %cnt_try% LEQ %max_try% (goto :7zip) else (
    echo Something was borked for "%~1"^! ^(%ERL%^) >> Compress.log
exit /B %ERL%

It's working fine for me with slightly different conditions (e.g. if "%%~xI" == ".txt")

... and I don't think that I've used any hacks :)

share|improve this answer
You should try and test out the script beforehand. – Insane Oct 15 '15 at 22:44
One might argue that calling a label and setlocal ENABLEDELAYEDEXPANSION are advanced functions.  BTW, why do you think you need delayed expansion? — I don't see where it is used. – Scott Oct 15 '15 at 22:52
@insane - now it's tested. – Limer Oct 15 '15 at 23:06
@scott - english isn't my native tongue. Is this better? ... in my opinion i didn't use any advanced functions or hacks – Limer Oct 15 '15 at 23:07
Thank you for testing it @Limer – Insane Oct 15 '15 at 23:11

Your Answer


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.