[英]How to improve performance of 3 successive GCI -Recurse calls?
Powershell 菜鸟在这里。 为了创建潜在重复目录的列表,我有一个循环在所有目录上运行以下 3 个 GCI 命令,以获取当前检查目录下的总大小、文件数和目录数:
$folderSize = Get-Childitem -Path $fullPath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue
$folderDirs = Get-ChildItem -Path $fullPath -Recurse -Force -ErrorAction SilentlyContinue -Directory | Measure-Object -ErrorAction SilentlyContinue
$folderFiles = Get-ChildItem -Path $fullPath -Recurse -Force -ErrorAction SilentlyContinue -File | Measure-Object -ErrorAction SilentlyContinue
代码运行良好,但在同一路径上使用 recurse 参数运行 3 次 GCI 似乎非常愚蠢。 获取给定目录的这 3 个信息的更有效方法是什么?
将 where first 查询的结果存储在一个变量中,使用.Where({})
扩展方法将它们拆分为基于PSIsContainer
属性的类别 - 此时您可以引用每个的自动Count
属性(而不是调用Measure-Object
用于简单计数项目的对象):
$allFileSystemItems = Get-Childitem -Path $fullPath -Recurse -Force -ErrorAction SilentlyContinue
$size = $allFileSystemItems |Measure-Object Length -Sum
# split collections into "directories" and "files"
$dirs,$files = $allFileSystemItems.Where({$_.PsIscontainer}, 'Split')
$dirCount = $dirs.Count
$fileCount = $files.Count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.