jeb wrote:But in line 2 the parser decide to use the content "XXX" of the outer loop.
But in line 3 the parser revert his decision and takes the content of the inner loop.
Yes, very interesting. I remember seeing you post something very similar before.
Another interesting quirk that I remember you (jeb) explained to me before - the FOR variable expansion is GREEDY
This can lead to unexpected behavior if we are not careful. Everything below is on the command line - no batch file involved.
Nothing special here:
Code: Select all
D:\utils>for /f %b in (""part1.part2"") do @echo %bPART3
"part1.part2"PART3
Now I want to eliminate the quotes from the display:
Code: Select all
D:\utils>for /f %b in (""part1.part2"") do @echo %~bPART3
part1.part2PART3
Still no problem
Now I want to do the same thing using a different variable:
Code: Select all
D:\utils>for /f %A in (""part1.part2"") do @echo %~APART3
\utils\RT3
It is actually logical. The FOR variable expansion supports a number of modifiers, including A for the file attribute and P for the file path. The file doesn't actually exist, so it only displays the path that was searched.
When parsing FOR variables after encountering %~, the parser looks for either a modifier or a variable. If it finds a variable that is also a valid modifier, it checks to see if the next character is a modifier or variable. It continues until it finds neither.
If we create the file, then the output makes more sense:
Code: Select all
D:\utils>echo junk>part1.part2
D:\utils>for /f %A in (""part1.part2"") do @echo %~APART3
--a------ \utils\RT3
This illustrates something that is new to me: The order of the expanded text is fixed. It does not matter what order the modifiers appear, the output will always have the same order.
It also doesn't matter how many times the modifier appears, and the DPNX modifiers are redundant in the presense of the F modifier:
Code: Select all
D:\utils>for /f %A in (""part1.part2"") do @echo %~^XNPDTZASFXNPDTZASFAOK-ENOUGH ALREADY
--a------ 08/02/2011 12:14 PM 6 D:\utils\PART1~1.PAROK-ENOUGH ALREADY
All the characters up until the O are valid modifiers. The A before the O is the variable. Note how the S modifier converts the full path into the short 8.3 format.
THIS STATEMENT IN RED IS WRONG!One other new discovery for me - A character is only considered a modifier if it matches the case of the FOR variable.The case of the modifier does not matter. I fooled myself with my testing. These statements don't have any modifiers
because there is not any valid FOR variable after the initial letter:
Code: Select all
D:\utils>for /f %A in (""part1.part2"") do @echo %~Apart3
part1.part2part3
D:\utils>for /f %a in (""part1.part2"") do @echo %~aPART3
part1.part2PART3
These statements DO have modifiers:
Code: Select all
D:\utils>for /f %A in (""part1.part2"") do @echo %~APART3
--a------ \utils\RT3
D:\utils>for /f %a in (""part1.part2"") do @echo %~apart3
--a------ \utils\rt3
Dave Benham