Recursively count files in subfolders

后端 未结 6 1849
耶瑟儿~
耶瑟儿~ 2020-12-29 10:08

I am trying to count the files in all subfolders in a directory and display them in a list.

For instance the following dirtree:

TEST
    /VOL01
              


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 10:42

    Something like this should work:

    dir -recurse |  ?{ $_.PSIsContainer } | %{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count }
    
    • dir -recurse lists all files under current directory and pipes (|) the result to
    • ?{ $_.PSIsContainer } which filters directories only then pipes again the resulting list to
    • %{ Write-Host $_.FullName (dir $_.FullName | Measure-Object).Count } which is a foreach loop that, for each member of the list ($_) displays the full name and the result of the following expression
    • (dir $_.FullName | Measure-Object).Count which provides a list of files under the $_.FullName path and counts members through Measure-Object

    • ?{ ... } is an alias for Where-Object

    • %{ ... } is an alias for foreach

提交回复
热议问题