有没有人知道一个 powershell 2.0 命令/脚本来计算特定文件夹中所有文件夹和子文件夹(递归; 不包括文件)的数量(例如 C:\folder1\folder2 中所有子文件夹的数量)?
此外,我还需要所有“叶子”文件夹的数量。换句话说,我只想计算没有子文件夹的文件夹。
有没有人知道一个 powershell 2.0 命令/脚本来计算特定文件夹中所有文件夹和子文件夹(递归; 不包括文件)的数量(例如 C:\folder1\folder2 中所有子文件夹的数量)?
此外,我还需要所有“叶子”文件夹的数量。换句话说,我只想计算没有子文件夹的文件夹。
在PowerShell 3.0中,你可以使用Directory开关:
(Get-ChildItem -Path <path> -Directory -Recurse -Force).Count
您可以使用get-childitem -recurse
获取当前文件夹中的所有文件和文件夹。
将其导入到Where-Object
中,以仅过滤那些是容器的文件。
$files = get-childitem -Path c:\temp -recurse
$folders = $files | where-object { $_.PSIsContainer }
Write-Host $folders.Count
一行代码实现:
(get-childitem -Path c:\temp -recurse | where-object { $_.PSIsContainer }).Count
(dir -rec | where-object{$_.PSIsContainer -and ((dir $_.fullname | where-object{$_.PSIsContainer}).count -eq 0)}).Count
如果您可以使用Powershell 3.0版本,它看起来会更加简洁:
(dir -rec -directory | where-object{(dir $_.fullname -directory).count -eq 0}).count
另一个选项:
(ls -force -rec | measure -inp {$_.psiscontainer} -Sum).sum
(gci -force -recurse | where-object { $_.PSIsContainer }).Count
.zip
文件。 我会测试并尝试发布更新...使用递归选项获取路径子项,将其传输到筛选器以仅保留容器,再次传输以测量项目计数
((get-childitem -Path $the_path -recurse | where-object { $_.PSIsContainer }) | measure).Count