Results 1 to 3 of 3

Thread: [RESOLVED] How to find the very first file created in a given directory

  1. #1

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Resolved [RESOLVED] How to find the very first file created in a given directory

    Hi all

    I have a five files in C:\test directory as shown below (with its creation time)

    File Name Creation Time
    --------- -------------
    File1.txt 22-APR-2006 17:25:01
    File2.txt 21-APR-2006 01:00:12
    File3.txt 20-APR-2006 07:11:00
    File4.txt 22-APR-2006 12:15:50
    File5.txt 22-APR-2006 09:35:32

    I want to find the very first file created in that directory. In the above example "File3.txt".

    How can I find that?

    Please help!

    Kanna.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: How to find the very first file created in a given directory

    Here's one way:
    VB Code:
    1. Public Function GetOldestFileFromDir( _
    2.   ByVal filePathAndPattern As String, _
    3.   Optional ByVal attributes As VbFileAttribute = vbNormal) As String
    4.  
    5.   Dim strFile As String
    6.   Dim strOldest As String
    7.   Dim strPath As String
    8.  
    9.   ' Get the path being checked
    10.   strPath = Left(filePathAndPattern, InStrRev(filePathAndPattern, "\"))
    11.   ' Get the first file in the specified location
    12.   strFile = Dir(filePathAndPattern, attributes)
    13.   ' Loop while there are files
    14.   Do While Len(strFile) > 0
    15.     ' if there isn't an "oldest file" use this one
    16.     If Len(strOldest) = 0 Then
    17.       strOldest = strFile
    18.      
    19.     ' Otherwise, compare the date of this file to
    20.     ' the current "oldest" file and if this file is
    21.     ' older, make "it" the "oldest" file
    22.     ElseIf FileDateTime(strPath & strFile) < FileDateTime(strPath & strOldest) Then
    23.       strOldest = strFile
    24.     End If
    25.     ' Get the next file
    26.     strFile = Dir
    27.   Loop
    28.   ' Return the "oldest" file
    29.   GetOldestFileFromDir = strOldest
    30. End Function
    Example Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.   Debug.Print GetOldestFileFromDir("C:\Test\*.*")
    3. End Sub
    Regards,

    - Aaron.

  3. #3

    Thread Starter
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Re: How to find the very first file created in a given directory

    Great!

    But, most of the times not working properly.

    Any thoughts??????????????

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width