dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Date and Time issue when adding items to my ListView

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Resolved [RESOLVED] Date and Time issue when adding items to my ListView

    When I drag and drop a file onto my ListView I get the correct date and time stamp; it is the same as what Explorer shows and it is the same as it is for that file in the source directory but when I drag and drop a folder onto my ListView I do not get the correct date and time but rather I always get 12/30/1899 12:00 AM.

    I have doubled check and the FilePath and FileName arguments are correct in all cases

    Form Code

    Code:
    Private Sub AddNewListItem(FilePath As String, FileName As String)
     Dim FileSize As String
     Dim FileDate As String
     
     Dim dteCreate As Date
     Dim dteAccess As Date
     Dim dteModify As Date
    
     FileSize = GetFileSize(FilePath)
     GetFileTimes FilePath, dteCreate, dteAccess, dteModify, True
     FileDate = Format(dteModify, "m/d/yyyy h:mm AMPM")
       '
       '
    End Sub

    Module Code

    Code:
    Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
     (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
      ByVal dwShareMode As Long, ByVal NoSecurity As Long, _
      ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
      ByVal hTemplateFile As Long) As Long
    
    Public Declare Function GetFileTime Lib "kernel32" _
     (ByVal hFile As Long, lpCreationTime As FILETIME, _
      lpLastAccessTime As FILETIME, _
      lpLastWriteTime As FILETIME) As Long
    
    Public Declare Function FileTimeToLocalFileTime Lib "kernel32" _
     (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
    
    Public Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, lpFileSize As Currency) As Boolean
    
    Public Function GetFileTimes(ByVal strFile As String, _
                                 ByRef dteCreate As Date, _
                                 ByRef dteAccess As Date, _
                                 ByRef dteModify As Date, _
                                 ByVal blnUseLocalTime As Boolean) As Boolean
     
     Dim lngHandle         As Long
     Dim Create_Filetime   As FILETIME
     Dim Access_Filetime   As FILETIME
     Dim Modified_Filetime As FILETIME
     Dim TempFileTime As FILETIME
    
     GetFileTimes = False
     
     '
     ' Open the file.
     '
     lngHandle = CreateFile(strFile, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
        
     If lngHandle = 0 Then Exit Function
       
     '
     ' Get the times.
     '
     If GetFileTime(lngHandle, Create_Filetime, Access_Filetime, Modified_Filetime) = 0 Then
       Call CloseHandle(lngHandle)
       Exit Function
     End If
     
     '
     ' Close the file.
     '
     If CloseHandle(lngHandle) = 0 Then Exit Function
     
     '
     ' Convert to the local file system time.
     '
     If blnUseLocalTime Then
       Call FileTimeToLocalFileTime(Create_Filetime, TempFileTime)
       Create_Filetime = TempFileTime
     
       Call FileTimeToLocalFileTime(Access_Filetime, TempFileTime)
       Access_Filetime = TempFileTime
     
       Call FileTimeToLocalFileTime(Modified_Filetime, TempFileTime)
       Modified_Filetime = TempFileTime
     End If
     
     '
     ' Convert into dates.
     '
     dteCreate = FileTimeToDate(Create_Filetime)
     dteAccess = FileTimeToDate(Access_Filetime)
     dteModify = FileTimeToDate(Modified_Filetime)
    
     GetFileTimes = True
    End Function


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Date and Time issue when adding items to my ListView

    Quote Originally Posted by jmsrickland View Post
    ... but when I drag and drop a folder onto my ListView I do not get the correct date and time but rather I always get 12/30/1899 12:00 AM.
    That's because that code as written does not support the retrieval of a folder's timestamps. According to CreateFile's documentation:

    Quote Originally Posted by MSDN
    To open a directory using CreateFile, specify the FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFlagsAndAttributes.

    BTW, when the CreateFile function fails, its return value is INVALID_HANDLE_VALUE (-1), not 0. This line would therefore never catch CreateFile's failure:

    Code:
     If lngHandle = 0 Then Exit Function
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Date and Time issue when adding items to my ListView

    OK, thanks.

    I changed it to this:
    Code:
     Dim fs, f
    
     For n = 0 To Dir1.ListCount - 1
       FileName = Dir1.List(n)
    
       FilePath = FileName
       FileName = Mid(FileName, InStrRev(FileName, "\") + 1)
         
       Set fs = CreateObject("Scripting.FileSystemObject")
       Set f = fs.GetFolder(FilePath)
       FileDate = Format(f.DateCreated, "m/d/yyyy h:mm AMPM")
         '
         '
         '


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width