简体   繁体   English

如何提高 3 个连续 GCI -Recurse 调用的性能?

[英]How to improve performance of 3 successive GCI -Recurse calls?

Powershell noob here. Powershell 菜鸟在这里。 In order to create a list of potential duplicate dirs, I have a loop that runs the following 3 GCI commands on all directories to get the total size, number of files and number of directories below the currently examins dir:为了创建潜在重复目录的列表,我有一个循环在所有目录上运行以下 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       

The code is working fine but it seems really dumb to run 3 times a GCI with the recurse parameter on the same path.代码运行良好,但在同一路径上使用 recurse 参数运行 3 次 GCI 似乎非常愚蠢。 What would be a more efficient way to get those 3 informations for a given directory?获取给定目录的这 3 个信息的更有效方法是什么?

Store the results of the where first query in a variable, use the .Where({}) extension method to split them into categories based on the PSIsContainer property - at which point you can reference the automagical Count property of each (rather than invoking Measure-Object for the simple act of counting the items):将 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM