Find a Date & Time using GetFileTime (1 Viewer)

ian_w

Registered User.
Local time
Today, 17:15
Joined
Jun 13, 2006
Messages
64
I've written a File Scanner in Access 97 that records File name, path, size etc and put all the information in a database.

I've been trying to find the Date Last Modified but im not having anyluck.

Does anyone have a decent example of how it is written as I am aware that there are a few API's required before you get an actual date / time value.

So far I have the following API's but I can't work out what needs to be done..

Code:
Dim strHostname As String
Dim sfi As SHFILEINFO ' Passes the values of SHFILEINFO to the string
Dim FindFileInfo As WIN32_FIND_DATA

Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal hFindFile As Long, ByRef lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
Private Declare Function GetFullPathName Lib "kernel32.dll" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32.dll" (ByRef lpFileTime As FILETIME, ByRef lpSystemTime As SYSTEMTIME) As Long
Private Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpCreationTime As FILETIME, ByRef lpLastAccessTime As FILETIME, ByRef lpLastWriteTime As FILETIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (ByRef lpFileTime As FILETIME, ByRef lpLocalFileTime As FILETIME) As Long



'SHGetFileInfo constants.

Private Const MAX_PATH As Integer = 260                     'Defines the maximum path lengh.
Private Const MAXDWORD = &HFFFFFFFF
Private Const INVALID_HANDLE As Long = -1
Private Const SHGFI_DISPLAYNAME = &H100                     '  get display name
Private Const SHGFI_TYPENAME = &H400                        '  get type name
Private Const SHGFI_EXETYPE = &H2000                        '  return exe type
Private Const SHGFI_PIDL = &H8                              '  pszPath is a pidl
Private Const SHGFI_USEFILEATTRIBUTES = &H800
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10

Private Type FILETIME
    dwLowDateTime  As Long
    dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
   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 Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternativeFileName As String * 14
End Type

Private Type SHFILEINFO
   hIcon As Long                       '  out: icon
   iIcon As Long                       '  out: icon index
   dwAttributes As Long                '  out: SFGAO_ flags
   szDisplayName As String * MAX_PATH  '  out: display name (or path)
   szTypeName As String * 80           '  out: type name
End Type
 

DCrake

Remembered
Local time
Today, 17:15
Joined
Jun 8, 2005
Messages
8,632
Simple Software Solutions

Find attached a complete module to find various items. Copy this into your mdb as a new module. Don't forget to reference Microsoft Scripting for runtime.

CodeMaster::cool:
 

Attachments

  • ModFSO.txt
    3.1 KB · Views: 273

ian_w

Registered User.
Local time
Today, 17:15
Joined
Jun 13, 2006
Messages
64
Thanks for the reply,

Sorry, I should of mentioned that I am already using a FSO object to find the date out, problem is that on random files it error's which leaves me unable to continue my files indexing.

This is what I am currently using..

Code:
Private Sub Modified_Date(strPath As String, Filename As String, strDate As String)

Dim strfullpath As String

If Filename = ".." Then

strDate = "N/A"

    Exit Sub

Else

strfullpath = strPath & Filename

strDate = FileDateTime(strfullpath)

End If

End Sub

On random files it will not find the information it should, out of 88,000 files one of them was an issue :mad:

I've looked into using the OnError function so if it errors it will pop a msg box up saying "%file% could not be scanned, a log will be generated" and then continue scanning but the search function on this site hasn't been working for me today?
 

ecawilkinson

Registered User.
Local time
Today, 17:15
Joined
Jan 25, 2005
Messages
112
use this structure:
Code:
sub subname()
on error goto err_handler ' err_handler is just a user-defined name, no set in stone

' code goes here

exit_sub:
    exit sub
err_handler:
    if err.number = [enter the errror number you get] then
        msgbox ""%file% could not be scanned, a log will be generated"
        Call LogGeneratorProcedure(%File%)
    else
        msgbox "Error Number: " & err.number & vbnewline & "Description: " & err.description
    end if

    resume exit_sub
end sub
That should get you started. There are other ways of doing this, try googling Error Handling VBA.

HTH,Chris
 

Users who are viewing this thread

Top Bottom