Progressive Variable: How to write a loop in a batch script

225 pts.
Tags:
Batch jobs
CLI
Scripting
Windows 7 Professional
Zip Files
I have written a batch script that the 7-zip CLI for Windows to split a file into volumes and send it to me via sendEmail.

The issue I have run into is how to write a loop that inputs a new variable for each volume without knowing how many volumes there will be.

For example; 7-zip creates MYFILE.7z.001 then *.002, *.003, etc.

I need to be able to change the last 3-digit extension to the next interval until there is no more to grab. Something like when 'if exist' fails, terminate the loop. Any suggestions?

Thank you.



Software/Hardware used:
Windows 7 Professional 64-bit

Answer Wiki

Thanks. We'll let you know when a new response is added.

There are several ways to accomplish this in a Windows command script. The simplest is to use the FOR file iterator command.

<pre>FOR %%a IN (“PATH\MYFILE.7z.*”) DO Some Work on %%a</pre>

Using FOR in this way will step through each file name matching the pattern. If you need more than one line of work code use a CALL and subroutine passing the filename. I would use a subroutine for a production script so I could log the actions and results for troubleshooting.

Other alternatives are the FOR numeric iterator using a IF NOT EXIST check and exiting the loop or SET /a and manually looping with a GOTO using an IF NOT EXIST check again.

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Techy1984
    So for my example something like; :A 7z a -v80m -t7z -mx0 -mmt=4 MYFILE.7z "C:MYFILE" sendEmail -f -t -s -a "C:MYFILE.7z.001" FOR %%a IN ("C:MYFILE.7z.*) Do sendEmail -f-t-s-a "C:MYFILE.7z.%%a" goto :A Might work? Just want to make sure I understand how you are using the FOR statement. Thank you.
    225 pointsBadges:
    report
  • Pjb0222
    You do not need the GOTO. The FOR loop steps through each file name for you. The %%a holds the fully qualified file name. Use "C:MYFILE.7z.*" for the file name in the FOR statement. Use an explicite path so that you do not have an unexected result. The nomenclature of C:FileName uses the currect working directory on C: as an implicite path to the file which can give interesting results. :-)
    7z a -v80m -t7z -mx0 -mmt=4 MYFILE.7z “C:MYFILE”
    FOR %%a IN (”C:MYFILE.7z.*") Do sendEmail -f-t-s-a “%%a”
    To see the FOR from a command line you use one "%" for the variable:
    FOR %a IN ("C:MYFILE.7z.*") Do ECHO %a
    3,310 pointsBadges:
    report
  • Pjb0222
    Arrg. The backslashes were dropped. Trying again with doubled to see if that takes... Example from a command line:
    FOR %a IN (”C:\MYFILE.7z.*”) Do ECHO %a
    3,310 pointsBadges:
    report
  • Techy1984
    LoL I did the same thing with the slashes but it looks like we get the idea. Thanks again for all your help I will give this a shot.
    225 pointsBadges:
    report
  • Techy1984
    Thanks! that worked beautifully I am now receiving pieces of my clients file and am able to get the whole 6+GB beast onto my machine easily. Thanks again.
    225 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following