Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find the a file's last modified date

Status
Not open for further replies.

croydon

Programmer
Apr 30, 2002
253
EU
How is it possible to find a file's last modified date without using FileSystemObject?

I ask this because my program has to run on XP and Win98, and so far as I am aware FSO will not run on Win98.

Any help would be appreciated.



 
Try thread222-707310.

At first glance it looks like what you need.

Matt
 
In my experience FSO runs just fine on Win98. Where did you hear that it doesn't?

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
MattSTech, that looks like what I need. Thanks.

Golom, when I tried it last year I received a run-time error. I'll try it again to find out what the error was. My application was developed on XP and installed as a runtime (with the appropriate dlls) on the Win98 PC.

 
Nevertheless, you can use GetFileTime API function to retrieve this information. This function retrieves creation date, last modification date and last access date for the given file handle.

The date information returned by this function is a Win32 FILETIME structure. You need to do some conversion to make it a VB Date data type for further use in VB code.
___
[tt]
Option Explicit
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Const GENERIC_READ = &H80000000
Private Const OPEN_EXISTING = 3
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 FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Sub Form_Load()
Dim dtCreated As Date, dtModified As Date, dtAccessed As Date
VBGetFileTimes "C:\AUTOEXEC.BAT", dtCreated, dtModified, dtAccessed
Debug.Print "Created:", dtCreated
Debug.Print "Modified:", dtModified
Debug.Print "Accessed:", dtAccessed
End Sub

Function VBGetFileTimes(FileName As String, Optional Created As Date, Optional Modified As Date, Optional Accessed As Date) As Boolean
Dim hFile As Long, ft1 As FILETIME, ft2 As FILETIME, ft3 As FILETIME
hFile = CreateFile(FileName, GENERIC_READ, 0, ByVal 0&, OPEN_EXISTING, 0, 0)
If hFile Then
GetFileTime hFile, ft1, ft2, ft3
CloseHandle hFile
Created = FileTimeToVBTime(ft1)
Accessed = FileTimeToVBTime(ft2)
Modified = FileTimeToVBTime(ft3)
VBGetFileTimes = True
End If
End Function

Private Function FileTimeToVBTime(ft As FILETIME) As Date
Dim lft As FILETIME, st As SYSTEMTIME
FileTimeToLocalFileTime ft, lft
FileTimeToSystemTime lft, st
FileTimeToVBTime = DateSerial(st.wYear, st.wMonth, st.wDay) + TimeSerial(st.wHour, st.wMinute, st.wSecond)
End Function[/tt]
 
Golom, the error I get on Win98 is 430 - Class does not support Automation or does not support expected interface.

Hypetia, thanks for the sample code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top