Powershell和Windows资源管理器属性具有不同的计数结果

问题描述

我递归计算对象(文件文件夹等)的总数,以检查文件夹及其Amazon S3备份。
当我在文件夹上使用Windows资源管理器(右键单击->属性)时,与以下Powershell代码生成的对象相比,我得到的对象总数更少。为什么?
Amazon S3有100%的时间与Windows资源管理器中的计数匹配。为什么Powershell的总数更高,可能的区别是什么(系统文件,隐藏文件等)?这些文件夹中的对象总数通常为77,000 +。

folder_name; Get-ChildItem -Recurse | Measure-Object | %{$_.count}

解决方法

我无法复制。

在文件浏览器中时,右键单击有问题的文件夹->属性 在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

您确定手动查看属性时是否同时在计算文件和文件夹?

,

差异来自Windows资源管理器中文件和文件夹的计数。 Powershell(2.0版内部版本6.1)将所有内容都计算在内。似乎-File和-Directory在PowerShell V2.0中不起作用。

我真的希望能够从大量文件夹中(递归地)获得一个列表,作为.cvs或.txt输出的列表。通过Windows资源管理器是一个接一个的过程,我没有得到可以复制/粘贴的输出。

,

要计算单独变量中文件和文件夹的数量,您可以

# 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"