我有2个文件夹。在这2个文件夹内有一些带有3个扩展名文件的文件(jpg,png,gif)。 我想计算这2个文件夹中有多少个文件并指定文件夹。
我希望我能得到这个结果
Folder1
.jpg = 12
.png = 2
.gif = 4
Folder2
.jpg = 6
.png = 4
.gif = 2
我尝试过此操作,但是我只计算那些文件夹中的所有文件,无法指定该文件夹。 任何人都可以提出想法,对此真的表示感谢。谢谢
$FindFolder = Get-ChildItem "D:\"
ForEach ($folder in $FindFolder)
{
$folder
Get-ChildItem -Name "D:\$folder" -Recurse -File -Include *.jpg, *.png, *.gif | Measure-Object | ForEach-Object{$_.Count}
}
答案 0 :(得分:3)
这应该为您提供类似的东西。这使用Group-Object
,更多详细信息可以为found here。 Select-Object
还使用calculated expressions来获取找到的文件的父文件夹。
您需要做的就是调整Get-ChildItem
以进行任意过滤!
$results = Get-ChildItem D:\ -Recurse -File | Select-Object Extension, `
@{Name="Parent"; Expression={[System.IO.Path]::GetDirectoryName($_.FullName)}}
$results | Group-Object Parent, Extension -NoElement | `
Select-Object Count, @{Name="Extension"; Expression={($_.Name -split ',')[0]}}, `
@{Name="Parent"; Expression={($_.Name -split ',')[1]}} | Sort-Object Count -Descending
输出:
Count Extension Parent
----- --------- ------
2274 .pdf D:\PDFBULK
1509 .doc D:\testFolder\SubFolder
402 .msg D:\testFolder\SubFolder
97 .msg D:\Marketing