Code Snippets → File System Object Code (last modified, etc.)

Get File Last Modified Date

Function GetLastModDate(strFilePathAndName As String) As Date
    'USAGE:
    ' Immediate Window: ?GetLastModDate "C:\Temp\MyFile.xls"
    ' Returns: 6/22/2010 4:37:46 PM
    
    Dim fso As Object
    Dim f As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set f = fso.GetFile(strFilePathAndName)
    GetLastModDate = f.DateLastModified
   
    Set fso = Nothing
End Function

Get File Created Date

Function GetCreatedDate(strFilePathAndName As String) As Date
    'USAGE:
    ' Immediate Window: ?GetLastCreatedDate "C:\Temp\MyFile.xls"
    ' Returns: 6/22/2010 4:37:46 PM
    
    Dim fso As Object
    Dim f As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set f = fso.GetFile(strFilePathAndName)
    GetCreatedDate = f.DateCreated
    Set fso = Nothing
End Function

Get File Size

Function GetFileSize(strFilePathAndName As String) As String
    'USAGE (returns in Megabytes):
    ' Immediate Window: ?GetFileSize "C:\Temp\MyFile.xls"
    ' Returns: 13.6 Mb
    
    Dim fso As Object
    Dim f As Object
    Dim fsize As Double
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set f = fso.GetFile(strFilePathAndName)
    fsize = (f.Size / 1024) / 1024
    
    GetFileSize = Round(fsize, 2) & " Mb"
   
    Set fso = Nothing
End Function

Get File Type

Function GetFileType(strFilePathAndName As String) As String
    'USAGE (returns the file type):
    ' Immediate Window: ?GetLastModDate "C:\Temp\MyFile.xls"
    ' Returns: Microsoft Excel Worksheet
    
    Dim fso As Object
    Dim f As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set f = fso.GetFile(strFilePathAndName)
    GetFileType = f.Type
   
    Set fso = Nothing
End Function