Return to Snippet

Revision: 61672
at January 4, 2013 20:26 by lolrenx


Initial Code
Public Function NewestFile(Directory As String, FileSpec As String) As String
' Returns the name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
' all files : "*.*"
    Dim FileName As String
    Dim MostRecentFile As String
    Dim MostRecentDate As Date
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
    FileName = Dir(Directory & FileSpec, 0)
    If FileName <> "" Then
        MostRecentFile = FileName
        MostRecentDate = FileDateTime(Directory & FileName)
        Do While FileName <> ""
            If FileDateTime(Directory & FileName) > MostRecentDate Then
                 MostRecentFile = FileName
                 MostRecentDate = FileDateTime(Directory & FileName)
             End If
             FileName = Dir
        Loop
    End If
    NewestFile = Directory & MostRecentFile
End Function

Initial URL


Initial Description
retrieves newest edited file in specified directory (optional file extension)

Initial Title
Newest File in directory

Initial Tags


Initial Language
Visual Basic