 |
|
 |
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
retrieve a file creation date
Hello all
I found at www.allapi.net a sample of "GetFileInformationByHandle" API function. Seem useful because returns a lot of info by a single API declaration. It uses a FILETIME type to return the creation date value. Here is the code: Private Const OPEN_EXISTING = 3
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type BY_HANDLE_FILE_INFORMATION
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
dwVolumeSerialNumber As Long
nFileSizeHigh As Long
nFileSizeLow As Long
nNumberOfLinks As Long
nFileIndexHigh As Long
nFileIndexLow As Long
End Type
Private Declare Function GetFileInformationByHandle Lib "kernel32" _
(ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) _
As Long
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 CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Sub Form_Load()
Dim hFile As Long, FileInfo As BY_HANDLE_FILE_INFORMATION
'create a handle to the file 'c:\autoexec.bat'
hFile = CreateFile("c:\autoexec.bat", 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
'retrieve the file information
GetFileInformationByHandle hFile, FileInfo
'close the handle
CloseHandle hFile
'show the result
MsgBox "File size: " + CStr(FileInfo.nFileSizeLow), vbInformation
End Sub |
Now, I'd like to retrieve the file creation date, but was not able to modify the above routine in order to get a formatted date from the FileInfo.ftCreationTime.dwLowDateTime (or .dwHighDateTime) variable. I tried adding the following lines mydate = FileInfo.ftCreationTime.dwHighDateTime
Debug.Print mydate
dmydate = CDate(mydate) ' ****
Debug.Print dmydate
MsgBox "File creation date: " & dmydate, vbInformation |
The row with the asterisks ' **** causes the error, though the previous Debug.Print line writes in fact a long integer number, as stored in the mydate var. I tried with CDate, CLng, adding the low and high part, ... but got only Overflows or Not-matching type errors, and didn't understand what the values returned by the FILETIME structure are.
I can't use the FileDateTime function, as it return the last modify date of the file, and I need the creation date.
Anybody can help me?
PS is there any place where I can find samples on API types' usage? I found many web pages about the API funcs, but none explaining the various items' meanings of the types used into them. Thankz And Regardz.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
23-02-2004 at 03:21 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: retrieve a file creation date
The FILETIME data structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601. So, you cannot just convert it (neither with Format function), you will get an overflow error, so you need to convert it to system time.
Add this to declarations:
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 Declare Function FileTimeToSystemTime Lib "kernel32" ( _
lpFileTime As FileTime, lpSystemTime As SYSTEMTIME) As Long |
And Form_Load event code:
Private Sub Form_Load()
Dim hFile As Long
Dim FileInfo As BY_HANDLE_FILE_INFORMATION
Dim SysTime As SYSTEMTIME
'create a handle to the file 'c:\autoexec.bat'
hFile = CreateFile("c:\autoexec.bat", 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
'retrieve the file information
GetFileInformationByHandle hFile, FileInfo
'close the handle
CloseHandle hFile
' Convert file time to system time
FileTimeToSystemTime FileInfo.ftCreationTime, SysTime
MsgBox "File created on " & DateSerial(SysTime.wYear, SysTime.wMonth, SysTime.wDay)
End Sub |
And, Yronium, for the question about API structures, the best place would be MSDN.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
24-02-2004 at 01:26 AM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: retrieve a file creation date
If you want to change created/modified/accessed date, then you can use SetFileTime API. I will post a quick example, beacuse it needs time to be converted to FileTime format (the same you have received in BY_HANDLE_FILE_INFORMATION structure) and, further more, you can convert it to GMT time to be accurate.
private Declare Function SetFileTime Lib "kernel32" Alias "SetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As LongPrivate Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
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 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 SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function SetFileTime 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 Sub Form_Load()
Dim CurrentDate As Date
Dim SysTime As SYSTEMTIME
Dim LocalTime As FILETIME
Dim GMTTime As FILETIME
Dim hFile As Long
CurrentDate = #1/1/2000#
SysTime.wYear = Year(CurrentDate)
SysTime.wMonth = Month(CurrentDate)
SysTime.wDay = Day(CurrentDate)
SysTime.wDayOfWeek = Weekday(CurrentDate) - 1
SysTime.wHour = Hour(CurrentDate)
SysTime.wMinute = Minute(CurrentDate)
SysTime.wSecond = Second(CurrentDate)
SysTime.wMilliseconds = 0
' Convert system time to local time
SystemTimeToFileTime SysTime, LocalTime
' Convert local time to GMT
LocalFileTimeToFileTime LocalTime, GMTTime
' Open file
hFile = CreateFile("c:\text.txt", GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
' Change created/modified/accessed time
SetFileTime hFile, GMTTime, GMTTime, GMTTime
' close the handle
CloseHandle hFile
End Sub |
lpCreationTime,lpLastAccessTime or lpLastWriteTime can be NULL if youdont want to change it.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
24-02-2004 at 03:48 PM |
|
|
|
|
 |
 |