Results 1 to 4 of 4

Thread: FileCopy NTFS to FAT32 changes file's modified date

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    FileCopy NTFS to FAT32 changes file's modified date

    I've just been bitten by an annoying quirk, experienced on 2 different WindowsXP systems.

    When copying a file from an NTFS partition to a FAT32 device, the new file's modified date is sometimes changed by 1 or 2 seconds.
    This became apparent in a file syncronization program I have that compares mod dates and copies newer files over the older, and in this case can never catch up since the mod date is a moving target.

    Only some files do this, and they do it consistently. It seems to have something to do with the original mod date value, maybe different number systems are rounding or something??

    I tried doing a drag-n-drop copy within Windows Explorer and it did the same thing.

    Anyone else see this behavior?

    DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: FileCopy NTFS to FAT32 changes file's modified date

    Forgot to add my little code snippet
    VB Code:
    1. Private Sub Command1_Click()
    2.     Const File1 = "C:\Junk.doc"
    3.     Const File2 = "E:\Junk.doc"
    4.  
    5.     FileCopy File1, File2
    6.     Debug.Print File1 & " "; FileDateTime(File1)
    7.     Debug.Print File2 & " "; FileDateTime(File2)
    8. End Sub
    9.  
    10. ' C:\Junk.doc 2/14/2006 7:52:30 PM
    11. ' E:\Junk.doc 2/14/2006 7:52:32 PM
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: FileCopy NTFS to FAT32 changes file's modified date

    Hi, I don't have a FAT drive here to replicate your problem, but a possible solution could be just get the file time before copying and then change the copied file time to that time. How to do this..

    In a Form..
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type FILETIME
    4.     dwLowDateTime As Long
    5.     dwHighDateTime As Long
    6. End Type
    7.  
    8. Private Type SYSTEMTIME
    9.     wYear As Integer
    10.     wMonth As Integer
    11.     wDayOfWeek As Integer
    12.     wDay As Integer
    13.     wHour As Integer
    14.     wMinute As Integer
    15.     wSecond As Integer
    16.     wMilliseconds As Integer
    17. End Type
    18.  
    19. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
    20.     ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, _
    21.     ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
    22.     ByVal hTemplateFile As Long) As Long
    23. Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, _
    24.     lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
    25. Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, _
    26.     lpFileTime As FILETIME) As Long
    27. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    28. Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, _
    29.     lpFileTime As FILETIME) As Long
    30.  
    31. Private Const GENERIC_WRITE = &H40000000
    32. Private Const OPEN_EXISTING = 3
    33. Private Const FILE_SHARE_READ = &H1
    34. Private Const FILE_SHARE_WRITE = &H2
    35.  
    36.  
    37. Private Sub ChangeFileTime(FilePath As String, NewDateModified As Date)
    38. Dim lngHandle       As Long
    39. Dim udtFileTime     As FILETIME
    40. Dim udtLocalTime    As FILETIME
    41. Dim udtSystemTime   As SYSTEMTIME
    42.  
    43.     With udtSystemTime
    44.         .wYear = Year(NewDateModified)
    45.         .wMonth = Month(NewDateModified)
    46.         .wDay = Day(NewDateModified)
    47.         .wDayOfWeek = Weekday(NewDateModified) - 1
    48.         .wHour = Hour(NewDateModified)
    49.         .wMinute = Minute(NewDateModified)
    50.         .wSecond = Second(NewDateModified)
    51.         .wMilliseconds = 0
    52.     End With
    53.  
    54.     ' convert system time to local time
    55.     SystemTimeToFileTime udtSystemTime, udtLocalTime
    56.     ' convert local time to GMT
    57.     LocalFileTimeToFileTime udtLocalTime, udtFileTime
    58.     ' open the file to get the filehandle
    59.     lngHandle = CreateFile(FilePath, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
    60.                            ByVal 0&, OPEN_EXISTING, 0, 0)
    61.     ' change date/time property of the file
    62.     SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
    63.     ' close the handle
    64.     CloseHandle lngHandle
    65. End Sub
    66.  
    67. Private Sub CopyFile(FromPath As String, ToPath As String)
    68. Dim TempDateTime As Date
    69.  
    70.     TempDateTime = FileDateTime(FromPath)
    71.     FileCopy FromPath, ToPath
    72.     ChangeFileTime ToPath, TempDateTime
    73. End Sub
    Then call it like this..
    VB Code:
    1. Private Sub Command1_Click()
    2.     Debug.Print FileDateTime("C:\Junk.doc") 'check the file time before copying
    3.     CopyFile "C:\Junk.doc", "E:\Junk.doc"
    4.     Debug.Print FileDateTime("E:\Junk.doc") 'check the copied file time, should be the same
    5. End Sub
    Last edited by jcis; Feb 15th, 2006 at 04:49 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: FileCopy NTFS to FAT32 changes file's modified date

    Thanks jcis,

    For now I simply put a 5-second fudge factor in the file comparison logic.
    i.e. to consider a file older or newer I insist on at least a 5 sec diff.
    The 5 seconds is an arbitrary number.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

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