Skip to main content

Wildcards in Windows Pathnames

Users have been asking for years for the ability to use wildcards in directory names as well as in the filenames. For example, to copy files whose name matches “foo*” and whose parent directory name matches “a*z”:

copy c:\myfiles\a*z\foo* d:\foo

Or to copy files whose name matches “foo*” in any subdirectory of c:\myfiles:

copy c:\myfiles\**\foo* d:\foo

These types of wildcards are common in Linux, but the dearth of Windows apps that support this syntax implies that Windows is not especially welcoming (OK, implacably hostile!) to filenames with wildcards in the pathname component. But after way too much pain and trial-and-error, we were able to implement directory wildcards in TCC 12.0 (for internal commands and functions only).

You can control the subdirectory recursion by specifying * or ** in the path. A * (or a partial name with a * and/or one or more ?’s) will match a single subdirectory level; a ** will match any all subdirectory levels for that pathname. You can also use regular expressions as directory wildcards.

Directory wildcards cannot be used with the /O:… option (which sorts entries before executing the command). And think very carefully before using directory wildcards with a /S (recurse subdirectories) option, as this will almost certainly return unexpected results!

There are a few commands and functions which do not support directory wildcards, as they would be meaningless or destructive (for example, TREE, @FILEOPEN, @FILEDATE, etc.).