borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (terminate an application)Next Topic (Saving picture box from extracted icon) New Topic New Poll Post Reply
AndreaVB Forum : API : retrieve a file creation date
Poster Message
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907

icon 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
View Profile Send Email to User Show All Posts | Quote Reply
thirumalaicb
Level: Sage

Registered: 09-09-2003
Posts: 51
icon Re: retrieve a file creation date

hi

will the following code


dim mydate as sting
mydate=format(FileInfo.ftCreationTime.dwHighDateTime, "dd/MM/yyyy")
debug.print mydate


solve your purpose.



____________________________
Life is at Stake; Make the most out of it!

23-02-2004 at 03:36 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: retrieve a file creation date

Thanks both, I'll give it a run.
Another question: the file I'm attempting to read creation date it's already open - I'm writing onto it, and I have to write in it's creation date too - so I'm asking if this game of Create/CloseHandle have any effect on it. What do you think, Goran? You are the API master here.

[Edited by yronium on 24-02-2004 at 02:21 PM GMT]

____________________________
Real Programmer can count up to 1024 on his fingers

24-02-2004 at 01:17 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
thirumalaicb
Level: Sage

Registered: 09-09-2003
Posts: 51
icon Re: retrieve a file creation date

hi Yoranium and Goran

It is indeed a wonderful experience for me to have an interaction with u who are such experienced in handling APIs. I will come back if I have any doubts. How u guys wont mind giving me advise or suggestion

Thanks a lot

____________________________
Life is at Stake; Make the most out of it!

26-02-2004 at 12:05 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: retrieve a file creation date

Goran:
no need of all the above amount of code, FileTimeToSystemTime works good to me, and I succeeded to store into the file its creation date, even if the file is currently being read by another process. Thanks again.
thirumalaicb:
I don't really think I deserve all your admiration, but I'll try to help you any time I can. You're welcome.


____________________________
Real Programmer can count up to 1024 on his fingers

27-02-2004 at 11:56 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : API : retrieve a file creation date
Previous Topic (terminate an application)Next Topic (Saving picture box from extracted icon) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder