Results 1 to 5 of 5

Thread: Get date and time of a file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Location
    Posts
    124

    Question

    Hi,

    Would anyone help me on how do I get the date & time of a file ? Had tried the API "GetFileTime" example, but failed to get it run.

    The first try I have got the error of "user-defined type may not passed by ByVal".

    I am new to VB, and would like to know what is the different between using the VB code "FileDateTime" and using the API function.

  2. #2
    Guest
    Do you have something similar to this?

    Code:
    Public Declare Function GetFileTime ........ ByVal lpszFileTime as FILETIME) As Long
    If so, take out the ByVal part before the lpszFileTime as FILETIME part and it should run (hopefully). The reason for this error is a bit obvious, User Defined Types (in the case FILETIME) can't be passed as a value, because it holds more than one (dwLowFileTime and dwHighFileTime). I think that's right I dunno.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Location
    Posts
    124
    Thanks.
    Do I need to use the "CreateFile" to open the file before I need to do use the "GetFileTime" ? If I look at the example, it seems like it involves more than one API function in order to get the date & time of the file.

  4. #4
    Guest
    Yeah that's correct, have a look at my module (or part of it):

    Code:
    'Module START
    Declare Function CreateFile Lib "kernel32.dll" 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
    Declare Function FileTimeToSystemTime Lib "kernel32.dll" (lpFileTime As FileTime, lpSystemTime As SystemTime) As Long
    Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FileTime, lpLastAccessTime As FileTime, lpLastWriteTime As FileTime) As Long
    
    Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Public Const OPEN_EXISTING = 3
    Public Const GENERIC_READ = &H80000000
    Public Const GENERIC_WRITE = &H40000000
    Public Const FILE_SHARE_READ = &H1
    Public Const FILE_SHARE_WRITE = &H2
    'Module END
    
    Public Function GetFileTimeB(strFileName As String)
    Dim sysTime As SystemTime
    Dim filTime(2) As FileTime
    Dim FinalString As String
    Dim hFile As Long
    
    hFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    
    If hFile = -1 Then
     'File couldn't be opened.
     Exit Function
    End If
    
    GetFileTime hFile, filTime(0), filTime(1), filTime(2)
    
    FileTimeToSystemTime filTime(0), sysTime 'Will only get Creation time.  If you want Last Modified or Accessed, replace filTime(0) with filTime(1) or filTime(2)
    
    FinalString = Format(sysTime.wHour & ":" & sysTime.wMinute & ":" & sysTime.wSecond, "hh:mm:ss") & ";   "
    FinalString = FinalString & Format(sysTime.wDay & "/" & sysTime.wMonth & "/" & sysTime.wYear, "dddd dd, mmmm yyyy")
    
    GetFileTimeB = FinalString
    
    CloseHandle hFile        'NEVER FORGET THIS!
    End Function
    
    Private Sub Command1_Click()
    Dim strString As String
    strString = "C:\WINDOWS\SNDVOL32.EXE"
    
    lblCreated.Caption = GetFileTimeB strString
    End Sub
    If all goes well, then lblCreated.Caption should equal a date, a readable one. If it fails, I'll e-mail you my project and you can work it out from that. It's not too complicated.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Location
    Posts
    124

    Smile

    it works as I am trying to run on the new project. I have got few few question below;

    i) Is the constant compulsory to be there ?
    ii) the time has been all the while 4 hours different from time the file was created.

    The project I am working on requires me to always monitor the interval of the file in a server since it was last modified. Any recommendation ?

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width