Solved

vb.net: list 10 most recent files

Posted on 2004-05-01
5
560 Views
Last Modified: 2012-08-14
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
Comment
Question by:kapcreations
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 10969058
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
 
LVL 1

Author Comment

by:kapcreations
ID: 10969375
huh?  I have never done a sorted list before...
0
 
LVL 27

Accepted Solution

by:
Dabas earned 250 total points
ID: 10969428
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now