Page 1 of 1

goto-jump destroys for-vars/loop

Posted: 30 Jan 2022 18:32
by hm1957

Code: Select all

@echo off
set List=A B C
for %%L in (%List%) do (
	echo outerL=%%L
	for %%T in (%%L) do (
		echo T > t.log
		:Testloop
		echo innerL=%%L
		if exist t.log (
			del t.log
			goto :Testloop
		)	
	)
)
Saved the code as Test.cmd

p:\> Test.cmd
outerL=A
innerL=A
innerL=%L
p:\>

Solved the real problem by using a recursive procedure instead of "Testloop".
But would to know why the outer for-loop gets canceled after doing one "goto :Testloop"

Thanks a lot in advance

Re: goto-jump destroys for-vars/loop

Posted: 30 Jan 2022 18:43
by Squashman
Batch file process from the top down. Once you break out of the FoR construct it finds the label from the top down and starts processing line by line again. If you need to break out of something without breaking the loop then use the CALL command to spawn out to a separate function.