[英]Improve Get-ChildItem funtion
我有以下脚本可以在大目录中搜索匹配特定参数的随机文件。 该脚本非常低效,并且有多个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
请求的数量,同时仍然实现提取满足每个集合参数的随机文件的相同目标?
如果它是您所追求的性能,那么快速而肮脏的方法是将一个完整的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 Green
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.