【问题标题】:powershell and windows explorer (properties) have different count resultspowershell 和 windows 资源管理器(属性)有不同的计数结果
【发布时间】:2021-01-23 20:29:33
【问题描述】:
我正在递归计算对象(文件、文件夹等)的总数,以检查文件夹与它们的 Amazon S3 备份。
当我在文件夹上使用 Windows 资源管理器时(右键单击 --> 属性),我得到的总对象数量少于以下 powershell 代码生成的数量。为什么?
Amazon S3 100% 的时间匹配来自 Windows Explorer 的计数。为什么 powershell 给出的总数更高,可能的区别是什么(系统文件、隐藏文件等)?这些文件夹中的对象总数通常为 77,000+。
folder_name; Get-ChildItem -Recurse | Measure-Object | %{$_.count}
【问题讨论】:
标签:
powershell
count
properties
powershell-2.0
windows-explorer
【解决方案1】:
要计算单独变量中的文件和文件夹的数量,您可以这样做
# create two variables for the count
[int64]$totalFolders, [int64]$totalFiles = 0
# loop over the folders in the path
Get-ChildItem -Path 'ThePath' -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.PSIsContainer) { $totalFolders++ } else { $totalFiles++ }
}
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"
-Force 开关确保隐藏文件和系统文件也被计算在内。
可能更快的替代方法是使用 robocopy:
$roboCount = robocopy 'ThePath' 'NoDestination' /L /E /BYTES
$totalFolders = @($roboCount -match 'New Dir').Count - 1 # the rootfolder is also counted
$totalFiles = @($roboCount -match 'New File').Count
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"
【解决方案2】:
差异来自 Windows 资源管理器计数文件和单独的文件夹。 Powershell(版本 2.0 Build 6.1)正在计算所有内容。似乎 -File 和 -Directory 在 PowerShell V2.0 中不起作用。
我真的希望能够以 .cvs 或 .txt 输出的形式从大量文件夹中获取仅文件数量(递归)的列表。逐个浏览 Windows 资源管理器,我没有将其作为可以复制/粘贴的输出。
【解决方案3】:
我无法复制。
在文件资源管理器中,右键单击有问题的文件夹 -> 属性
在General 选项卡下,有一个名为Contains 的部分。
这会将文件和文件夹列为单独的编号。
在我的示例中,我有 19,267 Files, 1,163 Folders,总共有 20,430 个对象
当我跑步时
Get-ChildItem -Path C:\folder -Recurse | measure | % Count
它返回20430
当我跑步时
Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $false} | measure | % count
它返回19267
当我跑步时
Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $true} | measure | % count
它返回1163
您确定在手动查看属性时同时计算文件和文件夹吗?