Get Oldest File From Directory
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 'File/Folder Handling - Get Oldest File From Directory Public Function GetOldestFileFromDir(ByVal filePathAndPattern As String, _ Optional ByVal attributes As VbFileAttribute = vbNormal) As String 'This function is used to get the oldest file from the given directory. Dim strFile As String Dim strOldest As String Dim strPath As String strPath = Left(filePathAndPattern, InStrRev(filePathAndPattern, "\")) strFile = Dir(filePathAndPattern, attributes) Do While Len(strFile) > 0 If Len(strOldest) = 0 Then strOldest = strFile ElseIf FileDateTime(strPath & strFile) < FileDateTime(strPath & strOldest) Then strOldest = strFile End If strFile = Dir Loop GetOldestFileFromDir = strOldest End Function 'How can I Call this function: 'Debug.Print GetOldestFileFromDir("C:\Temp\*.txt") |