goto-jump destroys for-vars/loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hm1957
Posts: 1
Joined: 30 Jan 2022 17:58

goto-jump destroys for-vars/loop

#1 Post by hm1957 » 30 Jan 2022 18:32

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: goto-jump destroys for-vars/loop

#2 Post by Squashman » 30 Jan 2022 18:43

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.

Post Reply