Solved

vb.net: list 10 most recent files

Posted on 2004-05-01
5
569 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

751 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