Link to home
Start Free TrialLog in
Avatar of christampa
christampa

asked on

File modified Date

Hello anyone know how to get the date/time a file was modified?  I know the FileDateTime function gets the date it was created, but need modified.  Thanks.

ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmwheeler
jmwheeler

To use aelatik's post you will need to add a Reference (Project -> References) to the 'Microsoft Scripting Runtime'.
jmwheeler,

CreateObject creates the object and does not need references
Option Explicit

Private Const GENERIC_READ = &H80000000
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIB_NORMAL = &H80

Private Type m_typFileTime
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type m_typSystemTime
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As m_typFileTime, lpSystemTime As m_typSystemTime) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, ByVal lpCreationTime As Long, ByVal lpLastAccessTime As Long, lpLastWriteTime As m_typFileTime) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

Private Function FileDateModified_Get(ByVal sFileFullname As String) As Date
' Returns the Modified Date of the passed file

Dim ftModified As m_typFileTime
Dim lpHandle As Long
Dim stModified As m_typSystemTime

On Error Resume Next

    lpHandle = CreateFile(sFileFullname, GENERIC_READ, 0&, 0&, OPEN_EXISTING, FILE_ATTRIB_NORMAL, ByVal 0&)
    If lpHandle > 0 Then
        Call GetFileTime(lpHandle, 0&, 0&, ftModified)
        Call FileTimeToSystemTime(ftModified, stModified)
        Call CloseHandle(lpHandle)

        With stModified
            FileDateModified_Get = CDate(.wDay & "/" & .wMonth & "/" & .wYear & " " & .wHour & ":" & .wMinute & ":" & .wSecond)
        End With
    End If

End Function

Private Sub Form_Load()
    MsgBox FileDateModified_Get("C:\myFile.txt")
End Sub