[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Oldest file from directory

Posted on 2006-04-27
4
Medium Priority
?
483 Views
Last Modified: 2012-08-14
Is there an easy way in vb.net to get a file from a directory of files that has the oldest time. As there are 100's of files coming in and I always will need only the oldest one of the files to process.  Thanks
0
Comment
Question by:LAppling7775
  • 3
4 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16555265
Hi LAppling7775;

This sample code will give you the file that is oldest created file in the directory.

        Dim path As String = "C:\Temp"
        Dim di As New DirectoryInfo(path)
        Dim fi() As FileInfo = di.GetFiles()
        Dim temp As DateTime = Now()
        Dim fOldest As FileInfo

        For Each f As FileInfo In fi
            If DateTime.Compare(f.CreationTime(), temp) < 0 Then
                fOldest = f
                temp = f.CreationTime()
            End If
        Next

        MessageBox.Show("The oldest file is = " & fOldest.FullName)

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16555304
Hi LAppling7775;

Use this version. If there are no files in the directory you do not want to process anything.

        Dim di As New DirectoryInfo("C:\Temp")
        Dim fi() As FileInfo = di.GetFiles()
        Dim temp As DateTime = Now()
        Dim fOldest As FileInfo
        Dim foundOne As Boolean = False

        For Each f As FileInfo In fi
            If DateTime.Compare(f.CreationTime(), temp) < 0 Then
                fOldest = f
                temp = f.CreationTime()
                foundOne = True
            End If
        Next

        If foundOne Then
            MessageBox.Show("The oldest file is = " & fOldest.FullName)
        Else
            MessageBox.Show("No files in the directory")
        End If


Fernando
0
 

Author Comment

by:LAppling7775
ID: 16561794
Thanks - will this work if there are several files stacking up - will it still get the oldest as 100's of files may be accumalating in the directory at once.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16562664
This will look at each file one by one comparing dates. When one is found to be older it will use that file as the oldest file and continue to compare to the rest. So in answer to your question yes.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 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