Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch FOR loop on range through command line

Tags:

I want to perform an operation multiple times from a command window. Common sense tells me that a FOR loop should be able to handle this. Sure enough, if I want to execute, say, myProg.exe, I can open a command window and use:

C:\> FOR %i in (1 2 3) DO myProg.exe 

Easy.

But what if I want to execute myProg.exe 1000 times? I want to specify a range in the FOR loop, but I'm having trouble seeing how to do this.

Intuitively, it seems like I should be able to do something like one of the following:

C:\> FOR %i in (1 to 1000) DO myProg.exe C:\> FOR %i in (1-1000) DO myProg.exe 

But, of course, this doesn't work. The FOR loop interprets the list as 3 tokens and 1 token, respectively, so myProg.exe is only executed 3 times and 1 time, respectively.


Batch File Solution

It'd probably be easy to write some sort of batch (.bat) file:

SET COUNT=0 :MyLoop     IF "%COUNT%" == "1000" GOTO EndLoop     myProg.exe     SET /A COUNT+=1     GOTO MyLoop :EndLoop 

But isn't there an easy way to do this from the command line?

like image 538
Kirby Avatar asked Mar 22 '13 18:03

Kirby


2 Answers

You can use the /l tag in your statement to make it loop through a set of numbers.

eg.

C:\> FOR /l %i in (1,1,1000) DO myProg.exe

This says loop through the range, starting at 1, stepping 1 at a time, until 1000

http://ss64.com/nt/for_l.html

like image 109
Jeremy Avatar answered Jan 15 '23 12:01

Jeremy


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

add another % sign before i to work

like image 40
Abhinav Banerjee Avatar answered Jan 15 '23 11:01

Abhinav Banerjee