Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

vb.net: list 10 most recent files

I have a directory with a lot of .log files, one for each day

how do I list just the 10 most recent files by file creation date, or, in other words, the files for the last 10 days?
0
kapcreations
Asked:
kapcreations
  • 2
1 Solution
 
DabasCommented:
Hi kapcreations:
I would start with code like this:

    Dim DirInfo As New System.IO.DirectoryInfo("")
    Dim Files() As System.IO.FileInfo = DirInfo.GetFiles("*.log")
    For Each f As IO.FileInfo In Files
         Save(f.Name, f.CreationTime())
    Next

Within Save you can create a SortedList and then keep only the most recent 10


Dabas
0
 
kapcreationsAuthor Commented:
huh?  I have never done a sorted list before...
0
 
DabasCommented:
kapcreations:
Ignore the For Each loop above and use this code instead:

        Dim sl As New SortedList
        For Each File As IO.FileInfo In Files
            sl.Add(File.CreationTime, File.Name) 'Add file to SortedList. SortKey is CreationTime
        Next

        Dim Count As Integer = 0
        For Each File As IO.FileInfo In sl 'Now show the sorted list in correct order
            MessageBox.Show(file.Name & " " & file.CreationTime.tostring)
        Next


Dabas
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now