改进 Get-ChildItem 函数

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