【发布时间】:2017-02-15 18:17:49
【问题描述】:
运行 Powershell V4.0。下面是在给定日期范围内按文件名中的日期戳搜索 1000 个文件并查找所有匹配字符串的脚本。但是 Get-ChildItem cmdlet 非常慢。我是 Powershell 的新手。有什么办法可以提高效率吗?或许分批阅读?
######################################################
#Date ranges and filename
$startdate = [datetime]'01/26/2017'
$enddate = [datetime] '02/05/2017'
$Filename = "DACNBA0124IDT030"
######################################################
#Progress
######################################################
$array =
do {
$startdate.ToString('yyyy_MM_dd*')
$startdate = $startdate.AddDays(1)
}
until ($startdate -gt [datetime] $enddate)
$files = $array | foreach-object {"G:\Live Engineering\AsRuns\Test\$_*"}
write-host $files
$Matches = get-childitem $files -recurse -force -OutBuffer 20000 | select- string $Filename | Where -Verbose {$_.line -notlike '*.mxf'}
$Matches.Matches.Count > "C:\Users\user\Desktop\report app\Log.txt"
【问题讨论】:
-
指定模式直接在get-child-item中查找select-string或
-exclude *.mxf文件。无论如何,最好看看您处理的文件的示例,它们的大小,是否是网络驱动器。 -
为了合理快速地搜索网络附加存储上的数十万个文件,我不得不切换到
cmd /c dir /b /s ...。它在我的网络上快了一个数量级。
标签: powershell-4.0 processing-efficiency get-childitem