Weird behavior of ~f modifier when path includes wildcards
Posted: 29 Apr 2020 22:24
I've long known that the ~f modifier used with parameter or FOR variable expansion is able to expand paths with wildcards into the first folder/file that it finds. The ~f modifier treats each node with wildcards within the source path separately when expanding wildcards, and works from left to right. If it finds a node with wildcards that cannot be matched to an existing file or folder, then that node and all subsequent nodes remain unchanged. (no wildcard expansion)
For example, if %1 is "c:\t*t\s*\*.txt" then "%~f1" could expand to "c:\test\subfolder\file.txt". But if the path is changed to "c:\t*t\z*\*.txt" and no folder exists beginning with z, then the expansion becomes "c:\test\z*\*.txt".
Not only does the ~f modifier understand * and ?, it also understands the poorly documented "<" and ">" wildcards.
All well and good. But then StackOverflow user Mofi pointed out some weird behavior to me at https://stackoverflow.com/questions/614 ... 4_61446617. It turns out that if a node consists of nothing but wildcards and possibly dots, then the ~f modifier does not look at the file system, but rather converts the wildcard node into a single dot
So modifying my earlier example a bit, "c:\t*t\?\*.*" becomes "c:\test\.\." Very weird. I was guessing it might have something to do with how Windows implements the backward compatibility hack of "dir *.*" being the same as "dir *". But then I discovered one last bit of weirdness - the wildcards are expanded normally if the node is the root node of a volume. For example, "c:\*" becomes "c:\$Recycle.Bin" on my machine. But "c:\*\*" becomes "c:\$Recycle.Bin\."
Dave Benham
For example, if %1 is "c:\t*t\s*\*.txt" then "%~f1" could expand to "c:\test\subfolder\file.txt". But if the path is changed to "c:\t*t\z*\*.txt" and no folder exists beginning with z, then the expansion becomes "c:\test\z*\*.txt".
Not only does the ~f modifier understand * and ?, it also understands the poorly documented "<" and ">" wildcards.
All well and good. But then StackOverflow user Mofi pointed out some weird behavior to me at https://stackoverflow.com/questions/614 ... 4_61446617. It turns out that if a node consists of nothing but wildcards and possibly dots, then the ~f modifier does not look at the file system, but rather converts the wildcard node into a single dot


So modifying my earlier example a bit, "c:\t*t\?\*.*" becomes "c:\test\.\." Very weird. I was guessing it might have something to do with how Windows implements the backward compatibility hack of "dir *.*" being the same as "dir *". But then I discovered one last bit of weirdness - the wildcards are expanded normally if the node is the root node of a volume. For example, "c:\*" becomes "c:\$Recycle.Bin" on my machine. But "c:\*\*" becomes "c:\$Recycle.Bin\."
Dave Benham