Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop a batch script only a certain amount of times?

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 :)

like image 776
user2838763 Avatar asked Oct 02 '13 13:10

user2838763


People also ask

How do you loop code in a batch file?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file. The above code is for Windows 2000, XP, and later users if you're running earlier Windows 98 or earlier you'd need to use the choice command.

What is %% A?

%%a are special variables created by the for command to represent the current loop item or a token of a current line. for is probably about the most complicated and powerful part of batch files. If you need loop, then in most cases for has you covered.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

How do you run a loop in CMD?

FOR /D - Loop through several folders. FOR /L - Loop through a range of numbers. FOR /F - Loop through items in a text file. FOR /F - Loop through the output of a command.


1 Answers

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.
like image 133
Edoardo Fiocchi Avatar answered Oct 13 '22 01:10

Edoardo Fiocchi