how to get DateLastModified name with wildcard

  • Thread starter Thread starter LeeL
  • Start date Start date
L

LeeL

I use code below to find the latest modified file in a directory. How can
can do that same thing an also set criteria to look for latest file modified
beginning with "aaa*" . . . how to get newest "C:\Lee\" & "aaa*"

Dim fNewest
Set oFolder = CreateObject("scripting.filesystemobject").getfolder("C:\Lee\")
For Each aFile In oFolder.Files
If fNewest = "" Then
Set fNewest = aFile
Else
If fNewest.DateLastModified < aFile.DateLastModified Then
Set fNewest = aFile
End If
End If
Next
MsgBox fNewest.Name
 
Try the below function...

Sub Macro()

MsgBox GetLatestFileName("c:\test")
MsgBox GetLatestFileName("c:\test", "ba*.txt")
End Sub

Function GetLatestFileName(strFolder As String, Optional strFilter As String)
Dim strFile As String, varDT As Variant

strFile = Dir(strFolder & "\" & strFilter, vbNormal)
Do While strFile <> ""
If FileDateTime(strFolder & "\" & strFile) > varDT Then
varDT = FileDateTime(strFolder & "\" & strFile)
GetLatestFileName = strFile
End If
strFile = Dir
Loop

End Function
 
Can the function include way to get other attributes such as: Datecreated,
DateLastModified, and DateLastAccessed?
 
Try the below function...

Sub Macro()

MsgBox GetLatestFileName("c:\test")
MsgBox GetLatestFileName("c:\test", "ba*.txt")
End Sub

Function GetLatestFileName(strFolder As String, Optional strFilter As String)
Dim strFile As String, varDT As Variant

strFile = Dir(strFolder & "\" & strFilter, vbNormal)
Do While strFile <> ""
If FileDateTime(strFolder & "\" & strFile) > varDT Then
varDT = FileDateTime(strFolder & "\" & strFile)
GetLatestFileName = strFile
End If
strFile = Dir
Loop

End Function

--
Jacob (MVP - Excel)
.

beauteous, thx.
 
Back
Top