2

I need to process files in order from oldest create date to newest.

I found the sort-object command which is what I need however I don't have a clue how to add it to my ForEach() statement.

Can someone show me how to do it?

Thanks

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
 }
Slinky
  • 1,027
  • 3
  • 15
  • 26

1 Answers1

4

You simply pipe the unsorted array to Sort-Object and iterate through the result like this:

(caret escapes and line breaks included for readability)

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
}

Note that the first line with the Get-ChildItem call in your code does nothing but display a sorted list of **.journal* files - this output is not processed any further in your script.

the-wabbit
  • 40,737
  • 13
  • 111
  • 174