Weird behavior of ~f modifier when path includes wildcards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dbenham
Expert
Posts: 2375
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Weird behavior of ~f modifier when path includes wildcards

#1 Post by dbenham » 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 :!: :shock:

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

penpen
Expert
Posts: 1809
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Weird behavior of ~f modifier when path includes wildcards

#2 Post by penpen » 30 Apr 2020 03:25

dbenham wrote:
29 Apr 2020 22:24
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 :!: :shock:
Why do you think it does not look at the file system?
Each subdirectory in windows contains the default directory "." (and ".."), which should be in most cases the first entry in the file system (but you shouldn't rely on that).


dbenham wrote:
29 Apr 2020 22:24
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\."
That is also expected, because root directories typically don't have the default directory "." (same with ".."), so the first entry there must be something else.


penpen

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

Re: Weird behavior of ~f modifier when path includes wildcards

#3 Post by dbenham » 30 Apr 2020 06:20

Oh my. Thanks penpen. That explains everything.

I'm so conditioned to ignore the . and .. DIR entries, I don't even see them. :roll: :oops:


Dave Benham

Post Reply