Jim
Jim

Reputation: 158

Files in directory sort by fileName ascending - for each

How to update my code so that all files are processed in order of date/time created?

Private Sub StartupFindExistingFiles(Path As String, SearchPattern As String)
    Dim fileEntries As String() = Directory.GetFiles(Path, SearchPattern)
    For Each fileName As String In fileEntries
        PrintJOBFile(fileName)
    Next
End Sub

Upvotes: 1

Views: 983

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460068

You could use LINQ with File.GetCreationTime:

Dim orderedByCreation = From file In Directory.EnumerateFiles(Path, SearchPattern)
                        Let createdAt = System.IO.File.GetCreationTime(file)
                        Order By createdAt Ascending
                        Select file

For Each filePath As String In orderedByCreation 
    PrintJOBFile(filePath)
Next

Note that you could also use GetFiles instead of EnumerateFiles. The latter can be more efficient.

Upvotes: 1

Related Questions