34

I need to run a windows command n times within a bat script file. I know how to do this in various programming languages but cannot manage to get it right on the windows command line :-(

I would expect something like either

for(int i = 0; i < 100; i++) {
   // do something
}

or even this (though not entirely seriously)

1.upto(100, {
   // do something
}) 

Thanks!

EDIT

I can write a program in java, perl, c or whatever that will generate a bat script that looks like this

for %%N in (1 2 3 4 5 6 7 8 9 10 11 12) do echo %%N

and so on. Or even "better":

echo 1
echo 2
echo 3
echo 4
echo 5
echo 6
echo 7
echo 8
echo 9
echo 10
echo 11
echo 12

and then execute it... But the thing is that I need a concise way to specify a range of numbers to iterate through within the script.

Thanks!

raoulsson
  • 4,633
  • 10
  • 32
  • 29
  • 1
    The answers here are perfectly good, but for the love of God...batch? Really? I would highly recommend moving to a more modern language. – EBGreen Aug 26 '09 at 14:22
  • It doesn't sound like he's got a choice in the matter. – wolfgangsz Aug 26 '09 at 15:20
  • I haven't seen anything that implies there is no choice. As a matter of fact he says he can write in "whatever" language, so I would say that without further information it sounds like he does have a choice. – EBGreen Aug 26 '09 at 15:39
  • Guys, I need to deploy code THAT is written in a modern language BY a script... – raoulsson Aug 26 '09 at 16:22
  • That still doesn't explain why **batch** scripting is required. – EBGreen Aug 26 '09 at 17:18
  • 1
    May it is or isn't required - sometimes it is the right tool for the job. You can't guarantee powershell, let alone bash, python, perl, etc. be installed on a Windows XP or 2003 server box. Batch is a perfectly acceptable solution. – Goyuix Aug 27 '09 at 18:04
  • Also, there is a clever trick in batch script that uses a sub-routine to call ping localhost in a loop, effectively pausing the script 1 second for each ping. It is a relatively simple (and portable) way to get the script to pause and leverage a loop as documented here. – Goyuix Aug 27 '09 at 18:06

3 Answers3

59

You can do it similarly like this:

ECHO Start of Loop

FOR /L %i IN (1,1,5) DO (
  ECHO %i
)

The 1,1,5 is decoded as:

(start,step,end)

Also note, if you are embedding this in a batch file, you will need to use the double percent sign (%%) to prefix your variables, otherwise the command interpreter will try to evaluate the variable %i prior to running the loop.

Winter
  • 103
  • 6
Goyuix
  • 3,164
  • 5
  • 28
  • 37
  • 3
    "ECHO Start of Loop" will be executed at each step, not just the start. – Dennis Williamson Aug 26 '09 at 14:23
  • 1
    Also, using an `@` sign will supress printing of the commands as they are executed from the command line. "... DO @( ..." – Dennis Williamson Aug 26 '09 at 14:28
  • This works only with the /L after the "FOR". Why's that? – raoulsson Aug 26 '09 at 14:52
  • 2
    Do a For /? and you will find: FOR /L %variable IN (start,step,end) DO command [command-parameters] The set is a sequence of numbers from start to end, by step amount. So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would generate the sequence (5 4 3 2 1) – EBGreen Aug 26 '09 at 15:03
  • 2
    It is better to use `%%i` in your text and add comment that it is `%i` for direct execution at command line. Most users will test it in BAT file and try to find the reason why they get error (like me). – i486 Sep 20 '18 at 09:42
13

Directly from the command line:

for /L %n in (1,1,100) do @echo %n

Using a batch file:

@echo off
for /L %%n in (1,1,100) do echo %%n

Displays:

1
2
3
...
100
jscott
  • 24,204
  • 8
  • 77
  • 99
fmoraesjr
  • 131
  • 1
  • 3
10

Syntax is

FOR %%A IN (1 2 3) DO ECHO %%A

Good article here and XP specifics here

Andy
  • 5,190
  • 23
  • 34
  • The examples using "for /L ..." are didactically more pleasant to me (expecially https://serverfault.com/a/59026/559613), but i like the external links you provided – grenix Feb 11 '20 at 13:53