7

How do I loop a batch script only a certain amount of times (x10 or something)? If the code was:

@echo off                                                                     
:loop1                                                                              
Start taskmgr.exe                                                       
Goto loop                                                                         
:loop2                                                                             
Start cmd.exe                                                                 
goto loop2     

How can loop loop1 and few times and go to loop2?

Any helpful answer would be appreciated :)

Jojo
  • 1,875
  • 3
  • 29
  • 29
user2838763
  • 71
  • 1
  • 1
  • 2
  • possible duplicate of [Loop through for loop %x% times in batch](http://stackoverflow.com/questions/15213375/loop-through-for-loop-x-times-in-batch) – Jon Carlstedt Oct 02 '13 at 13:31

5 Answers5

9

For a reason that i'm ignoring, the FOR command won't work for looping a specific label. For example (I may be wrong):

@echo off
for /L %%a in (1,1,2) do (
goto loop
)

:loop
echo this won't loop for 2 times.

This will simply loop infinite times. So, I have found an alternative simple method to loop a label as many times as I want. To do this, I create a variable, like loop that will have an even bigger number every time the label repeats.

There is an example:

@echo off
set loop=0
:loop
echo hello world
set /a loop=%loop%+1 
if "%loop%"=="2" goto next
goto loop

:next
echo This text will appear after repeating "hello world" for 2 times.

Output:

hello world
hello world
This text will appear after repeating "hello world" for 2 times.

Explanation:

  • set loop=0 sets the value of the variable loop at 0;
  • set /a loop=%loop%+1 adds 1 every time the label :loop is repeated.
  • if "%loop%"=="2" goto next tests if the variable loop is equal to 2 (so it was repeated for 2 times); if it's equal, it will go to the label :next, otherwise it will go to the label :loop.
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Edoardo Fiocchi
  • 141
  • 1
  • 7
  • in your first code snipped replace `goto loop` with `call :loop` (note: the `:` is optional for `goto`, but `call` needs it) and it will work. – Stephan Jun 16 '19 at 20:43
6

if you open a command window and type FOR /? it will give you the command you are looking for.

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)
Fred
  • 5,663
  • 4
  • 45
  • 74
5

Here is an example:

@echo off                                                                     
for /L %%a in (1,1,10) do (
Start taskmgr.exe          
)                                             

for /L %%a in (1,1,10) do (                                                        
Start cmd.exe                                                                 
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
2
@echo off
set /a a=1
goto loop
:loop
echo looped %a% times so far
set /a a=%a%+1
if %a%=10 (
echo looped a total of %a% times
)

You can also use this command:

@echo off
for /l %%a in (1,1,10) do (
rem %%a is a variable and starts at a value of 1, steps by 1 and ends at a value of 10
start "Command Prompt #%%a Opened" cmd
rem eg Command Prompt #1 Opened
rem eg Command Prompt #2 Opened
rem etc
)

which opens Command Prompt with the title "Command Prompt #%%a Opened". rem is a command that you can use to write comments.

0

Try this:

@echo off
set loopvar=1
:repeat
if %loopvar% gtr 5 (goto :done) else (set /a loopvar=%loopvar%+1 && echo Loop && goto :repeat)
:done

Under :done, write the code you want it to do after the loop. Make sure to replace five with the number of times you want the loop to repeat. For delay in the loop, try this:

@echo off
set loopvar=1
:repeatwithdelay
if %loopvar% gtr 5 (goto :done) else (set /a loopvar=%loopvar%+1 && timeout /T 1 >nul && echo Loop && goto :repeatwithdelay)

Hope it helped!

DCT A
  • 21
  • 5