Finding The Most Or Least Recent File In A Folder
This pages describes a VBA function that will return the name of the most recently or
least recently modified file in a specified folder. You can specify one or more file extensions
to include in the search.
.
This page describes a VBA function named GetRecentFile. This function will return the name of the
most recently modified or the least recently modified file in a specified folder. You can narrow the files to be examined by
specifying one or more file extensions or you can examine all files in the folder.
The procedure declaration is as follows:
Function GetRecentFile(DirPath As String, Extension As Variant, _
Optional LeastRecent As Boolean = False) As String
The input parameters to GetRecentFile are as follows:
DirPath
This is the folder name to search. This must be a fully qualified (drive and folder name) path and the folder must exist. If
the folder does not exist, the result of the function is vbNullString.
Extension
This specifies the extension(s) of the files to test. It may be (1) the string * or an empty
string (vbNullString) to search all files in the folder, (2) a single string specifying the
file extension (e.g., xls to look at Excel 97/2003 workbooks), (3) or an array of
one or more file extensions (e.g., Array ("xls","xlsm","xlsx") for Excel 97/2003 and 2007 workbooks).
LeastRecent
This indicates whether to return the name of the least recently (oldest) or the most recently (newest) modified file. If this
parameter is False or is omitted, the name of the most recently modified file is returned. If this parameter is True, the
name of the least recently modified file is returned.
This procedure searches only the folder named in DirPath. It does not search subfolders
of DirPath, subfolders of those subfolders, and so on. For a version of the code that
allows you to search a folder and all of its subfolders, see the section Recursive Version below.
You can download a module file containing the code below. The code for
the GetRecentFile function is shown below.
Function GetRecentFile(DirPath As String, Extension As Variant, _
Optional LeastRecent As Boolean = False) As String
Dim SaveDir As String
Dim FileName As String
Dim CompareDateTime As Double
Dim SaveFileName As String
Dim CurrFileDate As Double
Dim Ext As String
Dim CurrFileExt As String
Dim N As Long
Dim Pos As Long
Dim CompResult As Boolean
SaveDir = CurDir
On Error Resume Next
ChDrive DirPath
If Err.Number <> 0 Then
Exit Function
End If
ChDir DirPath
If Err.Number <> 0 Then
Exit Function
End If
If IsArray(Extension) = True Then
FileName = Dir(DirPath & "\*.*")
Else
If (StrComp(Extension, vbNullString, vbBinaryCompare) = 0) Or _
(StrComp(Extension, "*", vbBinaryCompare) = 0) Then
FileName = Dir(DirPath & "\*.*")
Else
FileName = Dir(DirPath & "\*." & Extension)
End If
End If
If LeastRecent = True Then
CompareDateTime = DateSerial(9999, 1, 1)
End If
Do Until FileName = vbNullString
FileName = DirPath & "\" & FileName
CurrFileDate = FileDateTime(FileName)
CompResult = False
If LeastRecent = True Then
If CurrFileDate < CompareDateTime Then
CompResult = True
Else
CompResult = False
End If
Else
If CurrFileDate > CompareDateTime Then
CompResult = True
Else
CompResult = False
End If
End If
If CompResult = True Then
Pos = InStrRev(FileName, ".")
If Pos > 0 Then
CurrFileExt = Mid(FileName, Pos + 1)
If IsArray(Extension) = True Then
For N = LBound(Extension) To UBound(Extension)
Ext = Extension(N)
If StrComp(Ext, CurrFileExt, vbTextCompare) = 0 Then
CompareDateTime = CurrFileDate
SaveFileName = FileName
End If
Next N
Else
If (StrComp(Extension, "*", vbBinaryCompare) = 0) Or _
(StrComp(Extension, vbNullString, vbBinaryCompare) = 0) Then
CompareDateTime = CurrFileDate
SaveFileName = FileName
Else
If StrComp(CurrFileExt, Extension, vbTextCompare) = 0 Then
CompareDateTime = CurrFileDate
SaveFileName = FileName
End If
End If
End If
End If
End If
FileName = Dir()
Loop
ChDrive SaveDir
ChDir SaveDir
GetRecentFile = SaveFileName
End Function
You can call the GetRecentFile function with code such as the following:
Sub AAA()
Dim FileName As String
Dim ModDate As Date
Dim FilePath As String
Dim Ext As Variant
FilePath = "C:\Temp"
Ext = Array("xls", "xlsm", "xlsx")
FileName = GetRecentFile(DirPath:=FilePath, Extension:=Ext, LeastRecent:=True)
If FileName = vbNullString Then
Debug.Print "No file found"
Else
ModDate = FileDateTime(FileName)
Debug.Print "File: " & FileName, "Modified: " & ModDate
End If
End Sub
You can download a module file containing the code above.
This version of the GetRecentFile recursively searches subfolders, their subfolders, and
so on. The function declaration is:
Function GetRecentFile(FolderName As String, _
Optional GetLeastRecent As Boolean = False, _
Optional Recurse As Boolean = False) As String
The parameters are:
FolderName is the name of the folder in which to start the search.
GetLeastRecent indicates whether to find the most recent (if False)
or the least recent (if True) file.
Recurse indicates whether to search subfolders (if True) or search
only FolderName (if False)
.
The recursive code is shown below. This code requires a reference to the scripting runtime library. In VBA,
go to the Tools menu, choose References and scroll down to Microsoft Scripting Runtime and
check that item.
Function GetRecentFile(FolderName As String, _
Optional GetLeastRecent As Boolean = False, _
Optional Recurse As Boolean = False) As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetRecentFile
' This function returns the full file name of the most or least
' recently modified file in the folder named in FolderName.
' Parameters:
' FolderName The name of the folder to search.
' GetLeastRecent If True, returns the file with the earliest
' modification date. If omitted or False, returns
' the file with the latest modification date.
' Recurse If False or omitted, only FolderName is searched.
' No subfolders are searched. If True, subfolders
' of FolderName and all child subfolders are searched.
' Return Value: The fully qualified file name of the file with
' most (or least) modification date.
' Required References:
' Microsoft Scripting RunTime
' GUID: {420B2830-E718-11CF-893D-00A0C9054228}
' Major: 1 Minor: 0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim FSO As Scripting.FileSystemObject
Dim RootFolder As Scripting.Folder
Dim RefFileName As String
Dim RefFileTime As Date
Dim F As Scripting.File
Dim SubF As Scripting.Folder
If Dir(FolderName, vbDirectory) = vbNullString Then
GetRecentFile = vbNullString
Exit Function
End If
If GetLeastRecent Then
RefFileTime = Now
Else
RefFileTime = 0
End If
Set FSO = New Scripting.FileSystemObject
Set RootFolder = FSO.GetFolder(FolderName)
For Each F In RootFolder.Files
If GetLeastRecent Then
If F.DateLastModified <= RefFileTime Then
RefFileTime = F.DateLastModified
RefFileName = F.Path
End If
Else
If F.DateLastModified >= RefFileTime Then
RefFileTime = F.DateLastModified
RefFileName = F.Path
End If
End If
Next F
If Recurse = False Then
GetRecentFile = RefFileName
Exit Function
End If
For Each SubF In RootFolder.SubFolders
DoSubFolder FSO, SubF, RefFileTime, RefFileName, GetLeastRecent
Next SubF
GetRecentFile = RefFileName
End Function
Sub DoSubFolder(FSO As Scripting.FileSystemObject, _
FF As Scripting.Folder, _
ByRef RefFileTime As Date, _
ByRef RefFileName As String, _
GetLeastRecent As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DoSubFolder
' This searches subfolders and their subfolders for the most
' (or least) recently modified file in the folder referenced
' by FF. It calls itself recursively to seach subfolders.
' Parameters:
' FSO A reference to an existing FileSystemObject.
' FF A reference to the folder to search
' RefFileTime A reference to the current most (or least)
' modification date.
' RefFileName A reference to the name of the most (or least)
' recently modified file.
' GetLeastRecent If True, searches for the oldest modified file.
' If False, searches for the newest modified file.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim F As Scripting.File
Dim SubF As Scripting.Folder
' search the files in folder FF
For Each F In FF.Files
If GetLeastRecent Then
If F.DateLastModified <= RefFileTime Then
RefFileTime = F.DateLastModified
RefFileName = F.Path
End If
Else
If F.DateLastModified >= RefFileTime Then
RefFileTime = F.DateLastModified
RefFileName = F.Path
End If
End If
Next F
' call itself for all subfolders of folder FF.
For Each SubF In FF.SubFolders
DoSubFolder FSO, SubF, RefFileTime, RefFileName, GetLeastRecent
Next SubF
End Sub
This page last updated: 8-September-2010