$folderCounts = @{}
Get-ChildItem -Path $PWD.Path -Recurse -Directory | ForEach-Object {
$fileCount = (Get-ChildItem -Path $_.FullName -File).Count
$folderCounts[$_.Name] = $fileCount
}
$folderCounts.GetEnumerator() | Out-File -FilePath (Join-Path $PWD.Path "folder_file_counts.txt")
使用方法:在当前路径地址栏中输入powershell,将代码复制回车后即在当前目录下生成结果。
如果要统计指定文件类型,就用下面这个
# 创建一个空的哈希表来存储文件夹名称和对应的PDF文件数量
$folderPdfCounts = @{}
# 获取当前路径下的所有文件夹(包括子文件夹)
Get-ChildItem -Path $PWD.Path -Recurse -Directory | ForEach-Object {
# 在每个文件夹中搜索以.pdf结尾的文件
$pdfFiles = Get-ChildItem -Path $_.FullName -Include *.pdf -File -Recurse
# 计算找到的PDF文件数量
$pdfCount = $pdfFiles.Count
# 如果该文件夹中有PDF文件,则将其名称和PDF文件数量添加到哈希表中
if ($pdfCount -gt 0) {
$folderPdfCounts[$_.Name] = $pdfCount
}
}
# 输出到控制台
foreach ($folder in $folderPdfCounts.Keys) {
Write-Host "Folder: $folder, Number of PDF files: $($folderPdfCounts[$folder])"
}
# 如果需要保存到文件中
$folderPdfCounts.GetEnumerator() | ForEach-Object {
Add-Content -Path (Join-Path $PWD.Path "folder_pdf_counts.txt") -Value "Folder: $($_.Key), Number of PDF files: $($_.Value)"
}
统计结果如下图所示:
点赞~