For loop syntax quirks

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: For loop syntax quirks

#16 Post by penpen » 02 Mar 2014 15:28

I could set the parser to a state where the delimiters could be escaped, at the cost of a single
leading " character..., so it is currently not of much (any) use, as it is then identically to a "default"
use of that characters, without the need to escape the delimiters.

Maybe another has an idea how to make real use of this;
I've added the working (but on different modes) values [1, 4], Ed's [2] and Jebs/Livius [3]
workarounds, which both works if not beeing used as an variable replacement argument in
delayed expansion. The for loop arguments 5 to 7 shows the default behavior within a
string [5], outside a string [6], and the escaped version [7].
It has also a modified version of Livius code for a testing the resulting strings:

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
set LF=^


@rem "=^"
set ^"v1=^"=^^^""
@rem a<lf>b
set ^"v2=a!LF!b"

for %%v in (
"=^^"
a^^^!LF^^^!b
^^^!"^!""=^^^^""^!"^^^!
"a!LF!b"
!"":"    a     , ; =^^^^"!""!
!" :"    a     , ; =^^^^"!" !
!" :"^ ^ a^   ^,^;^=^^^^^"!"!
) do (
   set "test="
   echo testing: %%v
   if !v1:%%v+!  == !v1:%v1%+! set "test=1" & echo(success:  v == "=^"
   if !v1:%%~v+! == !v1:%v1%+! set "test=1" & echo(success: ~v == "=^"
   if "!v2:%%v=+!"  == "+" set "test=1" & echo(success:  v == a^!LF^!b
   if "!v2:%%~v=+!" == "+" set "test=1" & echo(success: ~v == a^!LF^!b
   if not defined test echo(failed
   echo(   
)
endlocal
goto :eof

penpen

Post Reply