38

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!

7
  • 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, 2009 at 14:22
  • It doesn't sound like he's got a choice in the matter.
    – wolfgangsz
    Aug 26, 2009 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, 2009 at 15:39
  • Guys, I need to deploy code THAT is written in a modern language BY a script...
    – raoulsson
    Aug 26, 2009 at 16:22
  • 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, 2009 at 18:04

3 Answers 3

64

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.

5
  • 3
    "ECHO Start of Loop" will be executed at each step, not just the start. Aug 26, 2009 at 14:23
  • 1
    Also, using an @ sign will supress printing of the commands as they are executed from the command line. "... DO @( ..." Aug 26, 2009 at 14:28
  • This works only with the /L after the "FOR". Why's that?
    – raoulsson
    Aug 26, 2009 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, 2009 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, 2018 at 9:42
15

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
1
  • what if the number sequence is 0001 to 00100+ etc?
    – nwgat
    Aug 6, 2015 at 15:31
10

Syntax is

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

Good article here and XP specifics here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .