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



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.


  • 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
  • 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

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:


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.

  • 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
  • 1
    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
  • 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 9:42

Syntax is

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

Good article here and XP specifics here


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


  • what if the number sequence is 0001 to 00100+ etc? – nwgat Aug 6 '15 at 15:31

protected by Michael Hampton Mar 28 '14 at 0:15

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.