ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how to get DateLastModified name with wildcard (https://www.excelbanter.com/excel-programming/442345-how-get-datelastmodified-name-wildcard.html)

LeeL

how to get DateLastModified name with wildcard
 
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").getfold er("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

--
Thanks & Best Regards

Jacob Skaria

how to get DateLastModified name with wildcard
 
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)


"LeeL" wrote:

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").getfold er("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

--
Thanks & Best Regards


LeeL

how to get DateLastModified name with wildcard
 
Thanks! Nice and clean way to to do it!

--
Thanks & Best Regards


"Jacob Skaria" wrote:

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)


"LeeL" wrote:

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").getfold er("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

--
Thanks & Best Regards


LeeL

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

--
Thanks & Best Regards


"Jacob Skaria" wrote:

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)


"LeeL" wrote:

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").getfold er("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

--
Thanks & Best Regards


Chip Pearson

how to get DateLastModified name with wildcard
 
See http://www.cpearson.com/Excel/FileTimes.htm for downloadable code
that can get the create date, modify date, and access date for a file.

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]



On Thu, 6 May 2010 18:48:01 -0700, LeeL wrote:

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



All times are GMT +1. The time now is 05:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com