locked
count of files per directory - recurse RRS feed

  • Question

  • 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


    • Edited by joelsmit Thursday, July 6, 2017 9:34 PM
    Thursday, July 6, 2017 9:28 PM

Answers

  • 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


    \_(ツ)_/

    • Marked as answer by joelsmit Sunday, July 9, 2017 10:44 AM
    Saturday, July 8, 2017 12:06 PM

All replies

  • What have you tried?


    \_(ツ)_/

    Thursday, July 6, 2017 9:42 PM
  • Try

    ls | measure
    

    • Proposed as answer by JS2010 Friday, July 7, 2017 3:34 PM
    Friday, July 7, 2017 2:28 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:23 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

    • Edited by Dai Webb Friday, July 7, 2017 4:17 PM
    Friday, July 7, 2017 3:48 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
    		} 
    	}


    \_(ツ)_/

    • Proposed as answer by JS2010 Friday, July 7, 2017 6:11 PM
    • Unproposed as answer by JS2010 Friday, July 7, 2017 6:13 PM
    Friday, July 7, 2017 4:00 PM
  • Get-ChildItem counts more than files.  It will give wrong answers as will "ls | measure".


    \_(ツ)_/

    Friday, July 7, 2017 4:02 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 4:27 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() 

    • Proposed as answer by Martien van Dijk Friday, July 7, 2017 7:35 PM
    • Edited by JS2010 Friday, July 7, 2017 9:32 PM
    Friday, July 7, 2017 6:14 PM
  • 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

    Saturday, July 8, 2017 11:46 AM
  • 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


    \_(ツ)_/

    • Marked as answer by joelsmit Sunday, July 9, 2017 10:44 AM
    Saturday, July 8, 2017 12:06 PM
  • 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 10:47 AM
  • 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 7:12 PM
  • thank you! that did it
    Sunday, July 9, 2017 8:20 PM