How to create an infinite loop in Windows batch file?
Solution 1
How about using good(?) old goto
?
:loop
echo Ooops
goto loop
See also this for a more useful example.
Solution 2
Unlimited loop in one-line command for use in cmd
windows:
FOR /L %N IN () DO @echo Oops
Solution 3
A really infinite loop, counting from 1 to 10 with increment of 0.
You need infinite or more increments to reach the 10.
for /L %%n in (1,0,10) do (
echo do stuff
rem ** can't be leaved with a goto (hangs)
rem ** can't be stopped with exit /b (hangs)
rem ** can be stopped with exit
rem ** can be stopped with a syntax error
call :stop
)
:stop
call :__stop 2>nul
:__stop
() creates a syntax error, quits the batch
This could be useful if you need a really infinite loop, as it is much faster than a goto :loop
version because a for-loop is cached completely once at startup.
Solution 4
read help GOTO
and try
:again
do it
goto again
Solution 5
Another better way of doing it:
:LOOP
timeout /T 1 /NOBREAK
::pause or sleep x seconds also valid
call myLabel
if not ErrorLevel 1 goto :LOOP
This way you can take care of errors too
Related videos on Youtube
sooprise
I like to program, but am a noob, which is why I'm here. <-My sexy fatbooth picture
Updated on April 02, 2021Comments
-
sooprise about 3 years
This is basically what I want in a batch file. I want to be able to re-run "Do Stuff" whenever I press any key to go past the "Pause".
while(true){ Do Stuff Pause }
Looks like there are only
for
loops available and nowhile
loops in batch. How do I create an infinite loop then?-
thkala about 13 yearsAre you asking about a Windows/DOS batch file?
-
PA.+1 ooops, I posted without noticing the other answer!
-
-
Nicholi almost 12 yearsNice because it also works just typing it in at command prompt. for /L %n in (1,0,2) do @echo."hi guys"
-
jave.web over 10 yearsno good :D sometimes quicker but never good :D ok there is only one good place for goto - in the lowest programming x)
-
DoodleKana over 9 yearsI agree with jave.web you should use the for loop suggestion below. You should almost never use goto in your code even it is scripting. Goto was cool back in GW Basic days in 80s.
-
John Hunt almost 9 yearsI disagree with jave.web - there's nothing wrong with goto if you use it correctly. For example, I'm about to use it to ensure the program that is run within the loop auto-respawns if it dies for whatever reason. It may however be wise to put a small wait in the loop to stop cpu thrashing if my program dies instantly for some reason.
-
JimB over 8 years@theonlygusti Doesn't work as a one-line command, as you'd expect
:lbl & echo Ooops & goto lbl
; but @Nicholi 's comment below does:FOR /L (1,0,2) DO @echo Oops
-
Nabi K.A.Z. over 6 years@JimB your command has
(1 was unexpected at this time.
error, I thinks this is better: stackoverflow.com/a/8846529/1407491 -
JimB over 6 years@NabiK.A.Z. My mistake! Multiple typos, should be
FOR /L %N IN (1,0,2) DO ECHO %N
. Original was right. In a batch file, it needsFOR \L %%N
. I like this one for its simplicity. -
phrogg about 6 yearsTo use that in a batch file type: FOR /L %%N IN () DO @echo Oops
-
var firstName over 5 yearsThis is an under-rated answer. Thank you!
-
Nelda.techspiress over 4 yearsWish this solution had a little more explanation.
-
Martin Pecka over 3 yearsJust watch out for running commands which immediately exit, which is much more frequent on Windows than on Linux :-D
-
toddwz over 2 yearsI think the reason goto is not recommended is it could make code unreadable. This example of shows how to use goto correctly, and is simple, super clear, and straightforward to understand. Other answers using for require knowing the syntax of for in windows batch.