I have a project folder called topfolder
and I want to find all files in all subfolders that match a certain pattern, e.g. when the folder contains foo
anywhere in its name, I want to list all files inside it.
I tried something like:
gci .\topfolder -rec -filter *foo*
but it has two problems:
foo
will be listed (I want any file in foo-like folder).Then I tried this:
gci .\topfolder\*foo* -include *.*
but that doesn't work at all - apparently wildcards match only a single segment of a path so that pattern will match topfolder\foobar
but not topfolder\subfolder\foobar
.
The only way I've found so far was to use Get-ChildItem twice, like this:
gci .\topfolder -rec -include *foo* | where { $_.psiscontainer } | gci
This works but it seems like a waste to call gci
twice and I hope I just didn't find a right wildcard expression for something like -Path
, -Filter
or -Include
.
What is the best way to do that?
Get-ChildItem cmdlet in PowerShell is used to get items in one or more specified locations. Using Get-ChildItem, you can find files. You can easily find files by name, and location, search file for string, or find file locations using a match pattern.
The Get-ChildItem cmdlet uses the Path parameter to specify the Cert: provider. The Recurse parameter searches the directory specified by Path and its subdirectories.
The get-childitem cmdlet allows you to force information about hidden files or folders to be displayed. To display hidden files or folders, use the Force parameter with the get-childitem cmdlet. the hidden folders RECYCLER and System Volume Information are displayed in the results.
PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function.
I would use the Filter parameter instead of Include, it performs much fatser
gci .\topfolder -rec -filter *foo* | where { $_.psiscontainer } | gci
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With