goto within block forces var re-evaluation
Posted: 04 Feb 2013 10:09
Greetings!
I read an interesting Experts Exchange topic. Search for the Goto Issue section. The example given by Qlemo goes something like this:
Pretty cool, huh?
But wait, it gets better! I thought this trick would only work inside conditional or bare blocks, but FoxiDrive’s recent post on problem with a label in parentheses proves that you can use labels inside for loops so long as the label isn’t the last statement in the block. 
This leaves the door open to commit such attrocities as:
The above isn’t a standalone working example, but you get the idea.
Apologies to Jeb for mangling his “safe string return” code.
Hope you find this useful.
- SB
I read an interesting Experts Exchange topic. Search for the Goto Issue section. The example given by Qlemo goes something like this:
Code: Select all
set fruit=apples
if 1==1 (
echo(%fruit%
set fruit=bananas
goto nextline
:nextline
echo(%fruit%
)
Pretty cool, huh?


This leaves the door open to commit such attrocities as:
Code: Select all
setlocal enabledelayedexpansion
set "string=!%~1!" & set "substr=!%~2!"
for %%v in (string substr) do (
set "var=!%%v!"
for %%a in ("!LF!") do set "var=!var:%%~a=%%~L!"
for %%a in ("!CR!") do set "var=!var:%%~a=%%~3!"
if not defined notdelayed set "var=!var:^=^^^^!"
goto nextline
:nextline
if not defined notdelayed set "var=%var:!=^^^!%" !
set "%%v=!var!"
)
endlocal
The above isn’t a standalone working example, but you get the idea.

Hope you find this useful.
- SB