简体   繁体   中英

Powershell/windows 10: how to count files inside a folder?

I think it's best described with an example

parent1 (located inside my documents)
  - folder1
    - test11.png
    - test11.txt
    - folder11
      - test111.png
    - folder12
      - test121.png
      - test122.png
  - folder2
    - test21.png

if I run the command/script supplying the path and the extension, the output should be:

folder1: 4
folder2: 1

If I don't supply the extension, all files will be counted.

Any ideas on how this can be done?

Thanks

This can be done using Get-ChildItem and Measure-Object cmldets.

$RootDirectory = 'C:\Program Files (x86)'
Get-ChildItem $RootDirectory -Recurse -Directory | 
    ForEach-Object { 
        $DirectoryName = $_.FullName
        $FilesCount = Get-ChildItem -Path $DirectoryName -File | Measure-Object | Select -ExpandProperty Count 
        '{0}: {1}' -f $DirectoryName, $FilesCount
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM