how to get DateLastModified name with wildcard

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
 
J

Jacob Skaria

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
 
L

LeeL

Can the function include way to get other attributes such as: Datecreated,
DateLastModified, and DateLastAccessed?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top