Solved

vb.net: list 10 most recent files

Posted on 2004-05-01
5
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

623 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