Home > How-To Library > File/Folders Handling
Get Oldest File From Directory
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 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")
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.