我有以下脚本,它在一个大目录中搜索与特定参数匹配的随机文件。该脚本的效率非常低,并且有几个Get-ChildItem请求。我知道一定有更好的方法来做到这一点,也许是通过管道而不是多个独立的Get-ChildItem请求。
#begin random image pull for set1
#find MAIN images within date range
$set1Images = GCI $archive -recurse -include @("*MAIN*jpg") -exclude ("*Silhouette*") |
Where-Object {($_.LastWriteTime -ge $dateRangeBegin -and $_.LastWriteTime -le $dateRangeEnd -and $_.FullName -like "*\A6*") -or ($_.LastWriteTime -ge $dateRangeBegin -and $_.LastWriteTime -le $dateRangeEnd -and $_.FullName -like "*\A8*")} |
Get-Random -Count $count
$set1Images | Copy-Item -Destination $set1
Write-Host 'these are your images:' $set1Images -ForegroundColor Green
#begin random image pull for set2
#find MAIN images within date range
$set2Images = GCI $archive -recurse -include @("*MAIN*jpg") |
Where-Object {($_.LastWriteTime -ge $dateRangeBegin -and $_.LastWriteTime -le $dateRangeEnd -and $_.FullName -like "*\C*")} |
Get-Random -Count $count
$set2Images | Copy-Item -Destination $set2
Write-Host 'these are your images:' $set2Images -ForegroundColor Green
#begin random image pull for set3
#find MAIN images within date range
$set3Images = GCI $archive -recurse -include @("*MAIN*jpg") |
Where-Object {($_.LastWriteTime -ge $dateRangeBegin -and $_.LastWriteTime -le $dateRangeEnd -and $_.FullName -like "*\D*")} |
Get-Random -Count $count
$set3Images | Copy-Item -Destination $set3
Write-Host 'these are your images:' $set3Images -ForegroundColor Green有没有人可以帮我想出一个聪明的方法来减少Get-Childitem请求的数量,同时仍然实现相同的目标,即提取满足每组参数的随机文件?
发布于 2019-11-27 00:29:42
如果您追求的是性能,那么最快、最脏的方法就是使用日期范围筛选来执行一次完整的Get-ChildItem,并将其转换为一个变量。一旦你有了变量中的信息,你就可以对RAM中的变量执行3个单独的查询,因此执行起来会快得多。例如:
#Find ALL MAIN images within date range
$AllImages = GCI $archive -Recurse -include @("*MAIN*jpg") |
Where-Object {($_.LastWriteTime -ge $dateRangeBegin -and $_.LastWriteTime -le $dateRangeEnd)
#find MAIN images within date range not like '*Silhouette*'
$set1Images = $AllImages |
Where-Object { $_.FullName -notlike '*Silhouette*' -and ($_.FullName -like "*\A6*" -or $_.FullName -like "*\A8*")} |
Get-Random -Count $count
$set1Images | Copy-Item -Destination $set1
Write-Host 'these are your images:' $set1Images -ForegroundColor Green
#begin random image pull for set2
#find MAIN images within date range
$set2Images = $AllImages |
Where-Object { $_.FullName -like "*\C*" } |
Get-Random -Count $count
$set2Images | Copy-Item -Destination $set2
Write-Host 'these are your images:' $set2Images -ForegroundColor Green
#begin random image pull for set3
#find MAIN images within date range
$set3Images = $AllImages |
Where-Object { $_.FullName -like "*\D*" } |
Get-Random -Count $count
$set3Images | Copy-Item -Destination $set3
Write-Host 'these are your images:' $set3Images -ForegroundColor Greenhttps://stackoverflow.com/questions/59052544
复制相似问题