Search code examples
powershellwildcardget-childitem

Powershell Get-ChildItem extended (non * or ?) wildcard


I have no trouble with Get-ChildItem using * as a wildcard, but I wonder if it can be made to work with more refined wild cards. Given a file like C:\Folder\journal.0001.txt I would want to use the wildcard C:\Folder\journal.####.txt to get all "regular" journal files, but skip the ones named with this format journal.0000.worker1.log. Using the wildcard in the path throws an error that the path doesn't exist, and replacing the file bit with a simple * and the using journal.####.txt as a filter or include doesn't work. I do see that journal.????.txt works, but that would potentially grab journal.ABCD.txt should it exist. And I haven't even started playing with character sets.


Solution

  • Compared to RegEx wildcard patterns have a limited metacharacter set, and no quantifiers I know of, It does support character ranges like:

    Get-ChildItem C:\temp\06-11-21\journal.[0-9][0-9][0-9][0-9].txt
    

    Oddly, this only works as part of the path. It doesn't seem to work when specified as an argument to -Filter or -Include.

    about_Wildcards

    You can post filter the files using Where-Object or a For-EachObject loop with RegEx if you need more flexibility.