File Attributes Function

L

LeeL

Hello,

Jacob Skaria recently provided the very helpful function pasted below. I
have a follow-up question, can the function also include a way to get other
attributes such as: Datecreated, DateLastModified, and DateLastAccessed?

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
 
J

Javed

Hello,  

Jacob Skaria recently provided the very helpful function pasted below.  I
have a follow-up question, can the function also include a way to get other
attributes such as:  Datecreated, DateLastModified, and DateLastAccessed?

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

Probably No

You may have to use FileSystemObject.

Ofcourse I will seek Jacob's view
 
C

Chip Pearson

J

Jacob Skaria

As Javed mentioned I have modified using FSO..

Sub Macro()

Dim objFile As Object

'Set objFile = GetLatestFileName("c:\test")
Set objFile = GetLatestFileName("c:\test", "ba*.txt")

MsgBox "Latest File : " & objFile & vbCrLf & _
"Created : " & objFile.DateCreated & vbCrLf & _
"Modified : " & objFile.DateLastModified & vbCrLf & _
"Accessed : " & objFile.DateLastAccessed


End Sub

Function GetLatestFileName(strFolder As String, Optional _
strFilter As String) As Object
Dim fso As Object, objFold As Object, objFile As Object
Dim varDT As Variant, blnFilter As Boolean

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFold = fso.GetFolder(strFolder)

For Each objFile In objFold.Files

blnFilter = False
If strFilter = "" Then
blnFilter = True
Else
If LCase(objFile.Name) Like LCase(strFilter) Then blnFilter = True
End If

If blnFilter Then
If objFile.DateLastModified > varDT Then
varDT = objFile.DateLastModified
Set GetLatestFileName = objFile
End If
End If
Next

End Function
 

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