user1008764
user1008764

Reputation: 408

Recursively counting files of folder trees

Scenario: Files and Folders:

/folder1
  |-file
  |-file
  |-folder2
     |-file
     |-file
     |-folder3
        |-file
/folder4
  |-file
  |-folder5
     |-file
     |-file
     |-file
     |-file
     |-folder6
         |-file
         |-file
         |-file

What I now want to do is recursivly count all files in all subfoders, but in my output I just want to have the top foldernames and the file counts (of each file in the folder and all subfolder).

For this scenario it would be:

folder1;5
folder4;8

Is there a smart way to solve this with PowerShell?

Upvotes: 1

Views: 794

Answers (1)

arco444
arco444

Reputation: 22821

You can do:

Get-ChildItem -Directory | % {Write-Host -nonewline "$_;" ; $(Get-ChildItem $_ -Recurse -File).count } 

from the root of where you want to start the count.

The first command will get all the directories in the current directory, then for each of them you can get a count of all the files

Upvotes: 4

Related Questions