[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.
Re: How to find the very first file created in a given directory
Here's one way:
VB Code:
Public Function GetOldestFileFromDir( _
ByVal filePathAndPattern As String, _
Optional ByVal attributes As VbFileAttribute = vbNormal) As String
Dim strFile As String
Dim strOldest As String
Dim strPath As String
' Get the path being checked
strPath = Left(filePathAndPattern, InStrRev(filePathAndPattern, "\"))
' Get the first file in the specified location
strFile = Dir(filePathAndPattern, attributes)
' Loop while there are files
Do While Len(strFile) > 0
' if there isn't an "oldest file" use this one
If Len(strOldest) = 0 Then
strOldest = strFile
' Otherwise, compare the date of this file to
' the current "oldest" file and if this file is
' older, make "it" the "oldest" file
ElseIf FileDateTime(strPath & strFile) < FileDateTime(strPath & strOldest) Then
strOldest = strFile
End If
' Get the next file
strFile = Dir
Loop
' Return the "oldest" file
GetOldestFileFromDir = strOldest
End Function
Example Usage:
VB Code:
Private Sub Command1_Click()
Debug.Print GetOldestFileFromDir("C:\Test\*.*")
End Sub
Regards,
- Aaron.
Re: How to find the very first file created in a given directory
Great!
But, most of the times not working properly.
Any thoughts??????????????