The following works, but it returns the correct X in %%v and the correct Y in %%~v. My question is how to write one single for loop which retrieves the values correctly, and both in the same mode - either %%v or %%~v, but not one %%v and the other %%~v.
Code: Select all
@echo off & setlocal enableDelayedExpansion
@rem single linefeed char 0x0A (two blank lines required below)
set LF=^
@rem "=^"
set ^"v1=^"=^^^""
@rem a<lf>b
set ^"v2=a!LF!b"
echo(
for %%v in ( ^^^"^^^=^^^^^" "a!LF!b" ) do (
if '%%v'=='!v1!' echo v1 == v
if '%%~v'=='!v1!' echo v1 == ~v
if '%%v'=='!v2!' echo v2 == v & if not "!v2:%%v=+!"=="+" echo ...subst failed '!v2:%%v=+!' ??
if '%%~v'=='!v2!' echo v2 == ~v & if not "!v2:%%~v=+!"=="+" echo ...subst failed '!v2:%%~v=+!' ??
)
Code: Select all
v1 == v
v2 == ~v
The obvious escaping doesn't really work, in the sense that the 2nd variable containing the LF remains not fully evaluated (edit: i.e. contains a literal unexpanded !LF!, see jeb's clarification below http://www.dostips.com/forum/viewtopic.php?p=32773#p32773 /end edit) and fails inside a delayed expansion, as shown by the ?? lines in the output.
Code: Select all
echo(
for %%v in ( ^^^"^^^=^^^^^" a^^!LF^^!b ) do (
if '%%v'=='!v1!' echo v1 == v
if '%%~v'=='!v1!' echo v1 == ~v
if '%%v'=='!v2!' echo v2 == v & if not "!v2:%%v=+!"=="+" echo ...subst failed '!v2:%%v=+!' ??
if '%%~v'=='!v2!' echo v2 == ~v & if not "!v2:%%~v=+!"=="+" echo ...subst failed '!v2:%%~v=+!' ??
)
Code: Select all
v1 == v
v2 == v
...subst failed 'a
b' ??
v2 == ~v
...subst failed 'a
b' ??
Difficulty here seems to be that neither the = equal sign nor the <lf> linefeed seem to be readily escape'able outside quotes. And while it's foolish to ever say something is impossible in batch syntax but hints at why the above might not be technically workable would be appreciated, too. After all, this is just a curiosity, and not much of a practical matter.
Liviu