Pablo R
Pablo R

Reputation: 33

find and recurse

I have a folder structure d:\domains\<domain>\folder1\folder2\folderx

There are maybe 20 <domain> folders, with differing levels of folders below them.

I want to search all folders for .php files, and just print the unique <domain> folders where they exit.

So for example, if there are files found in

I just want domain1.com,domain2.com to be printed. It needs to work in PowerShell v2.

I have the following, but it only prints the first domain?

Get-ChildItem -Path @(Get-ChildItem -Path D:\domains | Where-Object {$_.PSIsContainer})[1].FullName -Recurse *.php |
    Select-Object -ExpandProperty FullName

Upvotes: 2

Views: 63

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Enumerate the domain folders, then filter for those of them that contain .php files.

Get-ChildItem 'D:\domains' | Where-Object {
    $_.PSIsContainer -and
    (Get-ChildItem $_.FullName -Filter '*.php' -Recurse)
}

If you have PowerShell v3 or newer you can use the -Directory switch instead of checking for $_.PSIsContainer:

Get-ChildItem 'D:\domains' -Directory | Where-Object {
    Get-ChildItem $_.FullName -Filter '*.php' -Recurse
}

Select the Name property from the output if you want just the folder/domain names:

... | Select-Object -Expand Name

Upvotes: 1

Related Questions