在循环之前对目录进行排序 - PowerShell [英] Sorting a Directory before Looping Through - PowerShell
本文介绍了在循环之前对目录进行排序 - PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要按从最旧的创建日期到最新的顺序处理文件
I need to process files in order from oldest create date to newest
这是正确的还是有更好的方法?
Is this correct or is there a better way to do it?
谢谢
Get-ChildItem -Path C:\Users\Tom\ -Filter "*.journal" | Sort-Object -Property CreationTime
ForEach ($sourcefile In $(Get-ChildItem $source | Where-Object { $_.Name -match "Daily_Reviews\[\d{1,12}-\d{1,12}\].journal" }))
{
#### Process files in order from oldest to newest
$file = $source+$sourcefile
}
推荐答案
简短回答:不,但您已经接近了.
Short answer: No, but you're close.
排序(和其他效果)仅在管道期间存在,除非您将结果存储在变量中.所以你排序的第一行不用于你的下一行.但是,您可以像这样组合它们:
Sorting(and other effects) only survive during a pipeline unless you store the result in a variable. So the first line where you sort is not used for you next line. However, you can combine them like this:
$source = "C:\Users\Tom\"
Get-ChildItem -Path $source -Filter "*.journal" |
Where-Object { $_.Name -match 'Daily_Reviews\[\d{1,12}-\d{1,12}\].journal' } |
Sort-Object -Property CreationTime | ForEach-Object {
#### Process files in order from oldest to newest
$_.FullName
}
这篇关于在循环之前对目录进行排序 - PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文