193

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?

Tommy Herbert
  • 20,407
  • 14
  • 52
  • 57
sooprise
  • 22,657
  • 67
  • 188
  • 276

6 Answers6

344

How about using good(?) old goto?

:loop

echo Ooops

goto loop

See also this for a more useful example.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • no good :D sometimes quicker but never good :D ok there is only one good place for goto - in the lowest programming x) – jave.web Aug 14 '13 at 13:37
  • 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. – DoodleKana Aug 22 '14 at 22:31
  • 20
    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. – John Hunt Jun 17 '15 at 10:27
  • 1
    @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` – JimB Sep 23 '15 at 19:58
  • @JimB your command has `(1 was unexpected at this time.` error, I thinks this is better: https://stackoverflow.com/a/8846529/1407491 – Nabi K.A.Z. Dec 30 '17 at 13:41
  • @NabiK.A.Z. My mistake! Multiple typos, should be `FOR /L %N IN (1,0,2) DO ECHO %N`. [Original](https://stackoverflow.com/questions/5487473/how-to-create-an-infinite-loop-in-windows-batch-file/5487520?noredirect=1#comment15033411_5488111) was right. In a batch file, it needs `FOR \L %%N `. I like this one for its simplicity. – JimB Dec 31 '17 at 14:38
  • 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. – toddwz Dec 02 '21 at 13:56
134

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

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

enter image description here

Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
72

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.

anonymoose
  • 819
  • 2
  • 11
  • 25
jeb
  • 78,592
  • 17
  • 171
  • 225
22

read help GOTO

and try

:again
do it
goto again
PA.
  • 28,486
  • 9
  • 71
  • 95
7

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

Julito Sanchis
  • 1,406
  • 2
  • 10
  • 10
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.

user1147015
  • 1,719
  • 1
  • 10
  • 2