我需要按照从最早的创builddate到最新的顺序处理文件。
我find了我所需要的sort-object命令,但是我不知道如何将它添加到我的ForEach()语句中。
有人可以告诉我怎么做吗?
谢谢
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 }
您只需将未sorting的数组input到Sort-Object,并像下面这样遍历结果:
(为了便于阅读,包括脱字符和换行符)
ForEach ($sourcefile In $(Get-ChildItem $source ^ | Where-Object { $_.Name -match "Daily_Reviews\[\d{1,12}-\d{1,12}\].journal" } ^ | Sort-Object -Property CreationTime)) { #### Process files in order from oldest to newest # Do-Whatever -With $sourcefile }
请注意,代码中的Get-ChildItem调用的第一行除了显示**。journal *文件的sorting列表之外什么都不做,这个输出在脚本中不再被处理。