views:

533

answers:

2

I need to execute a command 100-200 times, so far my research indicates that I would either have to copy paste 100 copies of this command, OR use a FOR loop, but the for loop expects a list of items, hence I would need 200 files to operate on, or a list of 200 items, hence defeating the point.

I would really not have to write a C program and go through the length of documenting why I had to write another program to execute my program for test purposes. Modification of my program itself is also no an option.

So, given a command how would I execute it times via a DOS batch script?

+11  A: 

for /l is your friend:

for /l %x in (1, 1, 100) do echo %x

Starts at 1, steps by one, and finishes at 100.

Use 2 %s if it's in a batch file

for /l %%x in (1, 1, 100) do echo %%x

(which is one of the things I really really hate about windows scripting)

If you have multiple commands for each iteration of the loop, do this:

for /l %x in (1, 1, 100) do (
   echo %x
   copy %x.txt z:\whatever\etc
)
Jon
Much better than my solution, provided we're talking about a reasonably modern DOS.
Carl Smotricz
@Carl I would hope that in 2010 you could at least assume NT Command Extensions are present :P
Jon
That depends on how literally we are to take the "DOS" reference. The latest version of MS-DOS, 7.1, dates back to 1997 and AFAIK doesn't have these extensions. The command shells delivered with Windows, on the other hand, of course do.
Carl Smotricz
Im using this to run a game repeatedly with a personal project of mine loaded, along with some statistics generation code. I should think anybody who cant run with a NT or later version of windows doesnt stand a chance at executing much else these days
Tom J Nowell
A: 

DOS doesn't offer very elegant mechanisms for this, but I think you can still code a loop for 100 or 200 iterations with reasonable effort. While there's not a numeric for loop, you can use a character string as a "loop variable."

Code the loop using GOTO, and for each iteration use SET X=%X%@ to add yet another @ sign to an environment variable X; and to exit the loop, compare the value of X with a string of 100 (or 200) @ signs.

I never said this was elegant, but it should work!

Carl Smotricz