Rob van der Woude's Scripting Pages

Batch How To ...

Use (and Break) Continuous or Endless Loops

With the FOR command you can create loops very much like For and For Each loops available in "true" scripting langauges.
Sometimes, however, we need a continuous loop, a.k.a. endless loop.
This is quite simple in batch, though it may not look intuitive (but what does in batch files?):

FOR /L %%A IN (0,0,1) DO (
	REM Do something
)

The only way to break out of this loop is pressing Ctrl+C followed by Y to confirm.

I tried to find a way out of the loop, i.e. an equivalent of the break command available in several other languages, or Exit For in VBScript.

Try and run the following code:

@ECHO OFF
SET LoopCount=0
FOR /F "tokens=2 delims=:.-" %%A IN ("%Time%") DO SET Minutes=%%A
FOR /L %%A IN (0,0,1) DO (
	SET /A LoopCount += 1
	SET LoopCount
	REM Try to break out of the loop as soon as the minute part of time has changed
	REM We cannot use %Time% here, as it would be interpreted at the start of the FOR loop and never change again
	FOR /F "tokens=2 delims=:.-" %%B IN ('TIME /T') DO (
		IF 1%%B NEQ 1%Minutes% (
			TITLE I want to get out
			GOTO OutOfLoop
		)
	)
	REM 1 second delay
	TIMEOUT.EXE /T 1 > NUL
)
:OutOfLoop
REM This text will never appear
ECHO You're out of the loop now

The FOR /F loops are intended to break the loop when the minute in the current time changes.
Well, after a couple of seconds, the console's title bar does state "I want to get out", but the text "You're out of the loop now" never appears, and the batch file seems to hang.

I tried EXIT /B and GOTO:EOF instead of GOTO OutOfLoop, but none of these delivered the expected result.
EXIT without /B does break the loop, but will also end the batch file, so the text "You're out of the loop now" will never be displayed.

It is possible to make it work as expected, but the code won't win a beauty contest:

@ECHO OFF
IF "%~1"==":Loop" GOTO :Loop
SET LoopCount=0
FOR /F "tokens=2 delims=:.-" %%A IN ("%Time%") DO SET Minutes=%%A

:: By using CMD /C to run the loop, the EXIT command will terminate the loop but not this batch file 
CMD.EXE /C "%~0" :Loop

ECHO You're out of the loop now
GOTO:EOF


:Loop
FOR /L %%A IN (0,0,1) DO (
	SET /A LoopCount += 1
	SET LoopCount
	FOR /F "tokens=2 delims=:.-" %%B IN ('TIME /T') DO (
		IF 1%%B NEQ 1%Minutes% (
			TITLE I want to get out
			EXIT
		)
	)
	REM 1 second delay
	TIMEOUT.EXE /T 1 > NUL
)

If you want the ability to break out of an "endless" loop programmatically and you want to keep it simple, forget about FOR and use a "classic" GOTO loop:

@ECHO OFF
SET LoopCount=0
FOR /F "tokens=2 delims=:.-" %%A IN ("%Time%") DO SET StartMinutes=%%A

:Loop
	SET /A LoopCount += 1
	SET LoopCount
	:: 1 second delay
	TIMEOUT.EXE /T 1 > NUL
	FOR /F "tokens=2 delims=:.-" %%A IN ("%Time%") DO SET CurrentMinutes=%%A
	IF 1%CurrentMinutes% EQU 1%StartMinutes% GOTO Loop
:: End of Loop

ECHO You're out of the loop now
Note: I used indented code for the loop, to make the code more readable.
An alternative would be comment lines at the start and end of the loop.

page last modified: 2022-11-17; loaded in 0.0021 seconds