XP bug on for ( var ) do %%~a strip quotes with ~

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

XP bug on for ( var ) do %%~a strip quotes with ~

#1 Post by Ed Dyreen » 29 Dec 2011 16:23

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. . .

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: XP bug on for ( var ) do %%~a strip quotes with ~

#2 Post by aGerman » 29 Dec 2011 17:13

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: XP bug on for ( var ) do %%~a strip quotes with ~

#3 Post by dbenham » 29 Dec 2011 17:16

It fails on Vista as well - and it's not a bug :wink:

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 %%! :!: :twisted:

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: XP bug on for ( var ) do %%~a strip quotes with ~

#4 Post by aGerman » 29 Dec 2011 17:21

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: XP bug on for ( var ) do %%~a strip quotes with ~

#5 Post by Ed Dyreen » 29 Dec 2011 17:22

dbenham wrote:But a is a valid FOR variable expansion modifier. So instead it interprets the construct as the file attributes of FOR variable %%! :!: :twisted:
:twisted: :twisted:

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 :mrgreen:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: XP bug on for ( var ) do %%~a strip quotes with ~

#6 Post by dbenham » 29 Dec 2011 17:32

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: XP bug on for ( var ) do %%~a strip quotes with ~

#7 Post by aGerman » 29 Dec 2011 17:34

Yes, I already found it. Thanks.

Regards
aGerman

Edit: Haha we should stop writing at the same time :D

Post Reply