Powershell统计每个文件夹下的文件数目

$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)"  
}

统计结果如下图所示:

点赞~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值