Page 1 of 1
XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 16:23
by Ed Dyreen
Code: Select all
@echo off
set $lf=^
:: two line
set ^"$n1c=^^^%$lf%%$lf%^%$lf%%$lf%^^"
:: Delim_, alphanumeric, splits on default delimiters
:: support : NADelayed, byVal, byRef, XP FOR /F: 'Kan bestand Ãüé??ú®?? niet vinden.' bug
:: (
%= =% set ^"Delim_=setLocal disableDelayedExpansion^&for %%^! in (1,2) do if %%^!==2 setlocal enableDelayedExpansion^&(%$n1c%
%= =%set ^"$=!$:^"=""!^"%$n1c%
%= =%set "$=!$:^=^^^^!"%= replace directly =%%$n1c%
%= =%call set "$=%%^$:^!=#"#"^!%%" !%$n1c%
%= =%set "$=!$:#"#"=^^^!"^&set ^"$=!$:""=^"!^"%$n1c%
%= =%set "?="^&for %%? in (!$!) do set "?=!?!¦%%?"%$n1c%
%= =%)^&for /f "delims=" %%^! in ("!$!") do for /f "tokens=1-26 delims=¦" %%a in ("!?!") do endlocal^&endlocal^&set "$p=%%!"^&set "$="^&"
:: )
set "$var=This ^Works !"
setlocal enableDelayedExpansion
:: (
%= =% set ^"@macro_=!Delim_! ( %$n1c%
%= =% setlocal enableDelayedExpansion %$n1c%
%= =% echo. %$n1c%
%= =% echo. Delim_: %$n1c%
%= =% echo. a=%%~a=^^^!%%a^^^!_ %$n1c%
%= =% echo. a=%%~a=^^^!%%~a^^^!_ This line is buggy %$n1c%
%= =% echo. b=%%~b=^^^!%%~b^^^!_ weird is normal %$n1c%
%= =% endlocal %$n1c%
%= =% ) else set $="
::
( %@macro_% $var, $var )
:: )
endlocal
pause
exit
I've claimed this before here, the evidence

Code: Select all
Delim_:
a=$var=This ^Works !_
a=$var=_ This line is buggy
b=$var=This ^Works !_ weird is normal
Druk op een toets om door te gaan. . .
Re: XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 17:13
by aGerman
I see the same on my Win7. %%~a is interpreted to expand the attributes. Try to start the loop with %%f instead of %%a and you'll see what stange things happen.
Regards
aGerman
Re: XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 17:16
by dbenham
It fails on Vista as well - and it's not a bug
The problem is your choice of
%%! as a FOR variable in your macro definition.
In the body of the macro, you expect
!%%~a! to be interpreted as the value of the variable specified by
%%~a. But
a is a valid FOR variable expansion modifier. So instead it interprets the construct as the file attributes of FOR variable
%%!
Remember that FOR variables are expanded before delayed expansion. So the FOR expansion sees
%%~a!.
!%%~b! works because
b is not a valid FOR variable modifier.
Dave Benham
Re: XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 17:21
by aGerman
dbenham wrote:The problem is your choice of %%! as a FOR variable in your macro definition.
I thought the same. But even if I changed the "!" to "I" it happened.
Regards
aGerman
Edit: Sorry I found my fault.
Re: XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 17:22
by Ed Dyreen
dbenham wrote:But
a is a valid FOR variable expansion modifier. So instead it interprets the construct as the file attributes of FOR variable
%%!

Code: Select all
%= =% set ^"Delim_=setLocal disableDelayedExpansion^&for %%? in (1,2) do if %%?==2 setlocal enableDelayedExpansion^&(%$n1c%
%= =%set ^"$=!$:^"=34###!^"%$n1c%
%= =%set "$=!$:^=^^^^!"%= replace directly =%%$n1c%
%= =%call set "$=%%^$:^!=""^!%%" !%$n1c%
%= =%set "$=!$:""=^^^!"%$n1c%
%= =%set ^"$=!$:34###=^"!^"%$n1c%
%= =%set "?="^&for %%? in (!$!) do set "?=!?!¦%%?"%$n1c%
%= =%)^&for /f "delims=" %%? in ("!$!") do for /f "tokens=1-26 delims=¦" %%a in ("!?!") do endlocal^&endlocal^&set "$p=%%?"^&set "$="^&"
Code: Select all
Delim_:
a=$var=This ^Works !_
a=$var=This ^Works !_ This line is buggy
b=$var=This ^Works !_
Druk op een toets om door te gaan. . .
What a fool I am

Re: XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 17:32
by dbenham
aGerman wrote:I thought the same. But even if I changed the "!" to "I" it happened.
The problem was the second FOR that used %%! at the bottom of the macro definition.
Never mind - I see you already figured it out.
Dave Benham
Re: XP bug on for ( var ) do %%~a strip quotes with ~
Posted: 29 Dec 2011 17:34
by aGerman
Yes, I already found it. Thanks.
Regards
aGerman
Edit: Haha we should stop writing at the same time
