Search code examples
powershellpowershell-core

Search through all folders recursively and find folder with the most files non-recursively


We have very matured file directory structure with an unknown depth. I would like to find the folder with the most files in it's root, preferably only PDF, DOC|DOCX, XLS|XLSX and PPTX|PPTS.

I have tried this

$files=@{}
Get-ChildItem -LiteralPath 'D:\' -Recurse -Directory -ErrorAction Ignore | Foreach {
    $files[$_.Fullname] = (Get-ChildItem -LiteralPath $_.Fullname -Recurse -File -ErrorAction Ignore).Count
} 
$files.GetEnumerator() | Sort Value -Descending -Top 1

from here https://superuser.com/a/970504/1252486 but it sums the file count recursively.

If I have this sample dir structure:

D:Tree
├───Dir1
│   │   File1.pdf
│   │   File2.pdf
│   │   File3.pdf
│   │
│   ├───Subdir1
│   │       Subfile1.pdf
│   │       Subfile2.pdf
│   │
│   └───Subdir2
│       │   Subfile1.pdf
│       │   Subfile2.pdf
│       │   Subfile3.pdf
│       │   Subfile4.pdf
│       │
│       └───Subsubdir1
│               Subsubfile1.pdf
│
├───Dir2
│   │   File1.pdf
│   │   File2.pdf
│   │   File3.pdf
│   │
│   ├───Subdir1
│   │       Subfile1.pdf
│   │       Subfile2.pdf
│   │       Subfile3.pdf
│   │
│   ├───Subdir2
│   │       Subfile1.pdf
│   │       Subfile2.pdf
│   │       Subfile3.pdf
│   │
│   └───Subdir3
│           Subfile1.pdf
│           Subfile2.pdf
│           Subfile3.pdf
│
└───Dir3

Tree\Dir2 has overall 12 Files, but only 3 files in it's root. Tree\Dir1\Subdir2 would here be the target.

Is there a neat short snippet for that?


Solution

  • Based on Group-Object

    Get-ChildItem D:\ -File -Recurse -Include *.pdf, *.doc |
        Group-Object DirectoryName | Sort-Object -Descending Count
    

    Based on a hashtable

    $Count = @{}
    Get-ChildItem D:\ -File -Recurse -Include *.pdf, *.doc |
        Foreach-Object { $Count[$_.DirectoryName]++ }
    $Count.GetEnumerator() | Sort-Object -Descending Value