How to create an infinite loop in Windows batch file?
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 no while
loops in batch. How do I create an infinite loop then?
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.