1

Is there a way to determine how wildcard matching is done in Get-ChildItem?

Various articles (1, 2) suggest that it is done through the WildcardPattern class, but I don’t think this is the case. For example, suppose you have a file in C:\test\test2\testfile.txt. Then Get-ChildItem –Path “C:\*\testfile.txt” will not find the file while WildcardPattern::IsMatch will. Wildcard "*" matching in Get-ChildItem seems to be on directory level: so "\*\" will never match more than one level, like "\A\B\".

So if WildcardPattern class isn't used, then what is?

Community
  • 1
  • 1
Davor Josipovic
  • 5,296
  • 1
  • 39
  • 57

3 Answers3

1

From what I know, it's using the WildcardPattern as you describe. However, the cmdlet Get-ChildItem limits it to the current directory (characters except \), so it won't conflict with the -Recurse switch that goes to unlimited levels.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frode F.
  • 52,376
  • 9
  • 98
  • 114
0

Either would work:

gci c:\test\*\testfile.txt

or

gci c:\*\testfile.txt -recurse

Example:

PS C:\temp\test2> dir


    Directory: C:\temp\test2


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          4/4/2013  10:41 PM          0 testfile.txt


PS C:\temp\test2> cd \
PS C:\> gci c:\*\testfile.txt -recurse -ea SilentlyContinue


    Directory: C:\Temp\test2


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          4/4/2013  10:41 PM          0 testfile.txt
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
  • 1
    This is incorrect. Please test before posting if you don't know for sure. – Davor Josipovic Apr 05 '13 at 07:11
  • @davor Actually, I did test them before posting. However, a "\s" was gone during the formatting of the first example, which is based on asker's directory structure. – Adil Hindistan Apr 06 '13 at 01:59
  • @adii `gci c:\test\*\testfile.txt` works as I stated in the question because `*` is limited to one directory level. `gci c:\*\testfile.txt -recurse` doesn't. The fact that it apparently works in your case makes it even more strange. Anyhow, the main question (i.e. what patternmatching is used) is still unanswered. – Davor Josipovic Apr 06 '13 at 07:32
0

With "C:\*\testfile.txt", the asterisk plays a role just for the first level directory (e.g test). The file you're looking for is not there and the output you get is expected. Add another asterisk for the second level and you'll get the desired output (e.g "C:\*\*\testfile.txt"). You can also add the Recurse switch to start searching from the current location, all the way downwards.

Shay Levy
  • 121,444
  • 32
  • 184
  • 206