203

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?

4
  • 6
    Are you asking about a Windows/DOS batch file? Commented Mar 30, 2011 at 14:12
  • Yeah, sorry for being vague, I'll make an addendum to the OP. Commented Mar 30, 2011 at 14:12
  • 1
    +1 ooops, I posted without noticing the other answer! Commented Mar 30, 2011 at 14:30
  • You can Use For Command Or Link Commented Aug 22, 2019 at 12:20

6 Answers 6

360

How about using good(?) old goto?

:loop

echo Ooops

goto loop

See also this for a more useful example.

Sign up to request clarification or add additional context in comments.

8 Comments

no good :D sometimes quicker but never good :D ok there is only one good place for goto - in the lowest programming x)
I 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.
I 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.
@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
I 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.
|
149

Unlimited loop in one-line command for use in cmd windows:

FOR /L %N IN () DO @echo Oops

enter image description here

2 Comments

To use that in a batch file type: FOR /L %%N IN () DO @echo Oops
Just watch out for running commands which immediately exit, which is much more frequent on Windows than on Linux :-D
75

A really infinite loop, counting from 1 to 10 with increment of 0.
You need infinite or more increments to reach the 10.

You can also use the short form: for ... in () do

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.

** pseudo infinite **
If you need a loop that is fast (then you shouldn't use GOTO), but the loop should be breakable, it's possible to use nested loops.

@echo off
setlocal EnableDelayedExpansion

set cnt=0
REM *** 6 nested loops, each count from 0 to 255
for /L %%L in (0 1 255) do for /L %%L in (0 1 255) do for /L %%L in (0 1 255) do for /L %%L in (0 1 255) do for /L %%L in (0 1 255) do for /L %%L in (0 1 255) do  (
    set /a cnt+=1
    echo Loop !cnt!
    if !cnt! == 10 goto :break
)
:break

It loops 2⁴⁸ (=281474976710656) times, but if the goto breaks the loops, it needs a maximum of 1536 empty loops.

2 Comments

Nice because it also works just typing it in at command prompt. for /L %n in (1,0,2) do @echo."hi guys"
Wish this solution had a little more explanation.
23

read help GOTO

and try

:again
do it
goto again

Comments

8

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

Comments

1

Here is an example of using the loop:

echo off
cls

:begin

set /P M=Input text to encode md5, press ENTER to exit: 
if %M%==%M1% goto end

echo.|set /p ="%M%" | openssl md5

set M1=%M%
Goto begin

This is the simple batch i use when i need to encrypt any message into md5 hash on Windows(openssl required), and the program would loyally repeat itself except given Ctrl+C or empty input.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.