简体   繁体   English

Powershell / Windows 10:如何计算文件夹中的文件?

[英]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. 可以使用Get-ChildItemMeasure-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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows10 / PowerShell:如何计算文件数,然后按子文件夹分类? - Windows10/powershell: how to count files and then sort them by subfolders? 如何使用 Powershell 计算文件夹和子文件夹中的文件 - How can I count files in folder and subfolder using Powershell 获取Windows 10中PowerShell中文件夹及其子文件夹中所有文件的时间戳列表 - Getting a list of timestamps of all files in a folder and its subfolder in PowerShell in Windows 10 如何在 Windows PowerShell 中比较两个 .zip 文件夹中的文件 - How to compare files in two .zip folder in Windows PowerShell 使用PowerShell对网络文件夹中的文件进行计数 - Count files in a network folder using PowerShell 如何使用 PowerShell 删除 windows 中的文件夹? - How to delete a folder in windows with PowerShell? Powershell - 删除过滤文件夹中的文件 - Powershell - Remove files inside the filtered folder Powershell替换文件夹中所有文件中的字符串 - Powershell to replace string in all files inside a folder 使用 Windows PowerShell 计算压缩文件中的行数 - Count lines in zipped files using Windows PowerShell 如何在 windows 的特定文件夹上自动运行 powershell cmd 10 延迟(30 秒)后启动 - How To Automatically run a powershell cmd on a specific folder on windows 10 start up after a delay (30s)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM