Search code examples
powershellwildcarddirectory-structurelsget-childitem

Powershell: multiple wildcards in Get-ChildItem path


I'm trying to use Get-ChildItem in powershell (I'm using the ls alias) to return only files within a specific folder structure.

As an example, say I'm trying to list all files whose names end in .foobar.txt that reside within a folder called src, which in turn resides in a folder called even. I've tried using the command ls -r *\even\src\*.foobar.txt | % FullName. I thought the first wildcard * in the path would allow any path that ends in \even\src\ and that the second one would match only files that end in .foobar.txt. But it isn't returning all the files I expect. Here's the structure of the directory I'm testing it in:

root
|- project1
|  |- odd
|     |- src
|     |  |- a.foobar.txt
|     |  |- b.foobar.txt
|     |  |- c.snafu.txt
|     |
|     |- temp
|        |- a.foobar.txt
|        |- b.foobar.txt
|        |- c.snafu.txt
|
|- project2
|  |- folder1
|     |- folder2
|        |- folder3
|           |- folder4
|              |- even
|                 |- src
|                 |  |- a.foobar.txt
|                 |  |- b.foobar.txt
|                 |  |- c.snafu.txt
|                 |
|                 |- temp
|                    |- a.foobar.txt
|                    |- b.foobar.txt
|                    |- c.snafu.txt
|
|- project3
|  |- odd
|     |- src
|     |  |- a.foobar.txt
|     |  |- b.foobar.txt
|     |  |- c.snafu.txt
|     |
|     |- temp
|        |- a.foobar.txt
|        |- b.foobar.txt
|        |- c.snafu.txt
|
|- project4
   |- even
      |- src
      |  |- a.foobar.txt
      |  |- b.foobar.txt
      |  |- c.snafu.txt
      |
      |- temp
         |- a.foobar.txt
         |- b.foobar.txt
         |- c.snafu.txt

The output of the above command is

root\project4\even\src\a.foobar.txt
root\project4\even\src\b.foobar.txt

However, I was expecting

root\project2\folder1\folder2\folder3\folder4\even\src\a.foobar.txt
root\project2\folder1\folder2\folder3\folder4\even\src\b.foobar.txt
root\project4\even\src\a.foobar.txt
root\project4\even\src\b.foobar.txt

How can I change my command so it also finds the files in the project2 folder?


Solution

  • Was thinking of string match:

    gci * -R | ? { $_.FullName -like '*\even\src\*.foobar.txt' }