English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

So one of my batch files just opens another program with a few arguments that are always the same. What I'd like it to do, however, is for one argument to change every time I run the batch file. Instead of always specifying the same file name, I'd like for it to increase by one every time it runs. So something like this:

Run batch file, batch file calls this:
programtorun.exe option1 FILE01 option2

Run batch file again, it runs this:
programtorun.exe option1 FILE02 option2

Run batch file a third time, it does this:
programtorun.exe option1 FILE03 option2

How can I get a batch file to just up the filename by 1 every time I run it? Or is it even possible?

Thanks!

2007-09-30 08:19:02 · 1 answers · asked by Doorrat 3 in Computers & Internet Programming & Design

1 answers

You can do this a number of ways in a batch file. How you do it will depend on how you're running your batch.

If you're running your "programtorun.exe" with different filenames immediately after each other, then you can do it using "for /L" to step through your incremental file names

for /L %i in (1,1,10) do (programtorun.exe option 1 file%i option2)

note: double up the % on variables when you run this command in a batch file, or,

for /L %%i in (1,1,10) do (programtorun.exe option 1 file%%i option2)

==================

If you're not running it immediately, but you are calling it within the same command window, then you can increment an environment variable to keep track of what file to call,

set count=1
programtorun.exe option1 FILE01 option2
set /a count=count+1
programtorun.exe option1 file%count% option2

note: you do not need to double %'s for environment variables when you call them in a batch
=====================

If you're going to run your batch at different times, and you may not be maintaining the same environment window, then you can use a flag file to keep track of your filename.

check for the filename in your flag file. If it isn't there, then run your program line and echo the filename into the flag file, and exit your batch. The next time the batch runs, it will drop to the first line that fails the filename test, run the program line, then exit the batch. and on and on.

find /i "file01" flag.txt programtorun.exe option1 FILE01 option2 & echo file01 >>flag.txt & goto :eof

find /i "file02" flag.txt && programtorun.exe option1 FILE02 option2 & echo file02 >>flag.txt & goto :eof

..etc

===============
Those are just a few ways that I can think of off the top of my head to do what you need. Hope those give you some ideas.

2007-09-30 14:39:29 · answer #1 · answered by Kevin 7 · 6 0

fedest.com, questions and answers