Share via


count of files per directory - recurse

Question

Thursday, July 6, 2017 9:28 PM

I am looking for a way to use Get-ChildItem to show number of files in each sub-directory in a path and list just the full path of the sub-directory and count of files in that sub-directory

example:

c:\dir1

25

c:\dir1\subdir2

423

c:\dir1\subdir9

795

c:\dir1\subdir2\subdir7

92

c:\dir1\subdir2\subdir3\dubdir12

11235

c:\dir2

11

c:\dir2\subdir3

45992

c:\dir2\subdir75

1156

c:\dir2\subdir75\dubdir147

13405

thank you

All replies (13)

Saturday, July 8, 2017 12:06 PM ✅Answered | 3 votes

I see everybody is still guessing:

Get-ChildItem d:\scripts -Directory -Recurse|
    ForEach-Object{
        [pscustomobject]@{
            FullName  = $_.Fullname
            FileCount = $_.GetFiles().Count
        }
    }

Try thinking about what each command and step is actually doing and why

\(ツ)_/


Thursday, July 6, 2017 9:42 PM

What have you tried?

\(ツ)_/


Friday, July 7, 2017 2:28 PM | 1 vote

Try

ls | measure

Friday, July 7, 2017 3:23 PM

Wow ... that's brilliant ... I could imagine - that does not leave any questions anymore for the OP. Thumbs up!

Grüße - Best regards

PS:> (79,108,97,102|%{[char]$_})-join''


Friday, July 7, 2017 3:48 PM

Try this:

$List = Get-ChildItem -Recurse -Directory "C:\MyFolder"
ForEach($Folder in $List)
{
   $Count = (Get-ChildItem -File $Folder.FullName).Count
   Write-Host $Folder.FullName "- $Count File(s)"
}

Regards,
Dave


Friday, July 7, 2017 4:00 PM

So far all attempts are wrong.

Consider the following and proceed from there:

Get-Item c:\Windows | 
    ForEach-Object{ 
        [pscustomobject]@{
            FullName = $_.Fullname
            FileCount = $_.GetFiles().Count
        } 
    }

\(ツ)_/


Friday, July 7, 2017 4:02 PM

Get-ChildItem counts more than files.  It will give wrong answers as will "ls | measure".

\(ツ)_/


Friday, July 7, 2017 4:27 PM

This is a bit more accurate as it returns exactly how many subfolders and files within:

$List = Get-ChildItem -Recurse -Directory "C:\Test"
ForEach($Folder in $List)
{
   $Count1 = (Get-ChildItem -Directory $Folder.FullName).Count
   $Count2 = (Get-ChildItem -File $Folder.FullName).Count
   Write-Host $Folder.FullName "- $Count1 Sub-folder(s), $Count2 File(s)"
}

And here is the output:

C:\Test\Folder1 - 0 Sub-folder(s), 1 File(s)
C:\Test\Folder2 - 0 Sub-folder(s), 4 File(s)
C:\Test\Folder3 - 2 Sub-folder(s), 0 File(s)
C:\Test\Folder3\Folder1 - 0 Sub-folder(s), 0 File(s)
C:\Test\Folder3\Folder2 - 0 Sub-folder(s), 2 File(s)


Friday, July 7, 2017 6:14 PM

Get-ChildItem -Recurse -Directory | 
ForEach {
  [pscustomobject]@{fullname=$_.fullname; numfiles=($_ | Get-ChildItem -File).count}
}

Pipe to format-list for long fullnames.  It depends on what you mean by the file count.

EDIT:

That's wierd, I can't figure out how to do:  "get-childitem -file" with the $_ on the right side.

maybe:   get-childitem -file $_.tostring() 


Saturday, July 8, 2017 11:46 AM

The measuring part of this proposal 

Get-ChildItem -Recurse -Directory | 
ForEach {
  [pscustomobject]@{fullname=$_.fullname; numfiles=($_ | Get-ChildItem -File).count}
}

didn't work for me. It led to exceptions, 'Property count not found', so I recurred to JS2010 first proposal and added the Measure cmdlet:

Get-ChildItem -Recurse -Directory | 
ForEach {
  [pscustomobject]@{fullname=$_.fullname; numfiles=($_ | Get-ChildItem -File | Measure).count}
}

and finally it works.

wizend


Sunday, July 9, 2017 10:47 AM

great stuff! thank you everyone

is it also possible (I am sure it is) to only show directories with more than X amount of files, for example 'count gt 100'?


Sunday, July 9, 2017 7:12 PM

Based on jrv's code, you could insert an additional condition:

Get-ChildItem  $env:est\Documents -Directory -Recurse| 
Where {$_.GetFiles().Count -gt 100} |
    ForEach-Object{
        [pscustomobject]@{
            FullName  = $_.Fullname
            FileCount = $_.GetFiles().Count
        }
    }

wizend


Sunday, July 9, 2017 8:20 PM

thank you! that did it