Batch script loop
Solution 1
for /l
is your friend:
for /l %x in (1, 1, 100) do echo %x
Starts at 1, steps by one, and finishes at 100.
WARNING: Use %%
instead of %
, if it's in a batch file, like:
for /l %%x in (1, 1, 100) do echo %%x
(which is one of the things I really really hate about windows scripting.)
If you have multiple commands for each iteration of the loop, do this:
for /l %x in (1, 1, 100) do (
echo %x
copy %x.txt z:\whatever\etc
)
or in a batch file
for /l %%x in (1, 1, 100) do (
echo %%x
copy %%x.txt z:\whatever\etc
)
Key:
/l
denotes that the for
command will operate in a numerical fashion, rather than operating on a set of files
%x
is the loops variable
(starting value, increment of value, end condition[inclusive] )
Solution 2
And to iterate on the files of a directory:
@echo off
setlocal enableDelayedExpansion
set MYDIR=C:\something
for /F %%x in ('dir /B/D %MYDIR%') do (
set FILENAME=%MYDIR%\%%x\log\IL_ERROR.log
echo =========================== Search in !FILENAME! ===========================
c:\utils\grep motiv !FILENAME!
)
You must use "enableDelayedExpansion" and !FILENAME! instead of $FILENAME$. In the second case, DOS will interpret the variable only once (before it enters the loop) and not each time the program loops.
Solution 3
Template for a simple but counted loop:
set loopcount=[Number of times]
:loop
[Commands you want to repeat]
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
Example: Say "Hello World!" 5 times:
@echo off
set loopcount=5
:loop
echo Hello World!
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
This example will output:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Press any key to continue . . .
Solution 4
You could also try this instead of a for
loop:
set count=0
:loop
set /a count=%count%+1
(Commands here)
if %count% neq 100 goto loop
(Commands after loop)
It's quite small and it's what I use all the time.
Solution 5
Or you can decrement/increment a variable by the number of times you want to loop:
SETLOCAL ENABLEDELAYEDEXPANSION
SET counter=200
:Beginning
IF %counter% NEQ 0 (
echo %x
copy %x.txt z:\whatever\etc
SET /A counter=%counter%-1
GOTO Beginning
) ELSE (
ENDLOCAL
SET counter=
GOTO:eof
Obviously, using FOR /L
is the highway and this is the backstreet that takes longer, but it gets to the same destination.
Tom J Nowell
I'm an elected community moderator at WordPress Stack Exchange. I also blog about WordPress and PHP at https://tomjn.com
Updated on July 08, 2022Comments
-
Tom J Nowell almost 2 years
I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a
for
loop, but thefor
loop expects a list of items, hence I would need 200 files to operate on, or a list of 200 items, defeating the point.I would rather not have to write a C program and go through the length of documenting why I had to write another program to execute my program for test purposes. Modification of my program itself is also not an option.
So, given a command,
a
, how would I execute itN
times via a batch script?Note: I don't want an infinite loop
For example, here is what it would look like in Javascript:
var i; for (i = 0; i < 100; i++) { console.log( i ); }
What would it look like in a batch script running on Windows?