首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改进Get-ChildItem函数

改进Get-ChildItem函数
EN

Stack Overflow用户
提问于 2019-11-26 21:56:52
回答 1查看 101关注 0票数 0

我有以下脚本,它在一个大目录中搜索与特定参数匹配的随机文件。该脚本的效率非常低,并且有几个Get-ChildItem请求。我知道一定有更好的方法来做到这一点,也许是通过管道而不是多个独立的Get-ChildItem请求。

代码语言:javascript
复制
#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请求的数量,同时仍然实现相同的目标,即提取满足每组参数的随机文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-27 00:29:42

如果您追求的是性能,那么最快、最脏的方法就是使用日期范围筛选来执行一次完整的Get-ChildItem,并将其转换为一个变量。一旦你有了变量中的信息,你就可以对RAM中的变量执行3个单独的查询,因此执行起来会快得多。例如:

代码语言:javascript
复制
#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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59052544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档