For Loop counting from 1 to n in a windows bat script
Solution 1
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.
Solution 2
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
Solution 3
Syntax is
FOR %%A IN (1 2 3) DO ECHO %%A
Good article here and XP specifics here
Related videos on Youtube
![raoulsson](https://i.stack.imgur.com/D8nV6.jpg?s=256&g=1)
raoulsson
CTO at Contovista AG Before: CEO at Zorp Technologies Inc., San Francisco, ...until Google Lens came out... Experienced software engineer and teamlead looking to build/enable useful, delightful, and meaningful products. Passionate, hard-worker interested in contributing to team-oriented, strong engineering cultures. Proven track record of hiring and running successful teams.
Updated on September 17, 2022Comments
-
raoulsson almost 2 years
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!
-
Srinivasan MK almost 15 yearsThe answers here are perfectly good, but for the love of God...batch? Really? I would highly recommend moving to a more modern language.
-
wolfgangsz almost 15 yearsIt doesn't sound like he's got a choice in the matter.
-
Srinivasan MK almost 15 yearsI 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.
-
raoulsson almost 15 yearsGuys, I need to deploy code THAT is written in a modern language BY a script...
-
Srinivasan MK almost 15 yearsThat still doesn't explain why batch scripting is required.
-
JSchlather almost 15 yearsMay 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.
-
JSchlather almost 15 yearsAlso, 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.
-
-
Dennis Williamson almost 15 years"ECHO Start of Loop" will be executed at each step, not just the start.
-
Dennis Williamson almost 15 yearsAlso, using an
@
sign will supress printing of the commands as they are executed from the command line. "... DO @( ..." -
raoulsson almost 15 yearsThis works only with the /L after the "FOR". Why's that?
-
Srinivasan MK almost 15 yearsDo 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)
-
nwgat almost 9 yearswhat if the number sequence is 0001 to 00100+ etc?
-
i486 almost 6 yearsIt 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). -
grenix over 4 yearsThe examples using "for /L ..." are didactically more pleasant to me (expecially serverfault.com/a/59026/559613), but i like the external links you provided