|
-
Feb 15th, 2006, 03:40 PM
#1
Thread Starter
Hyperactive Member
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
-
Feb 15th, 2006, 03:52 PM
#2
Thread Starter
Hyperactive Member
Re: FileCopy NTFS to FAT32 changes file's modified date
Forgot to add my little code snippet
VB Code:
Private Sub Command1_Click()
Const File1 = "C:\Junk.doc"
Const File2 = "E:\Junk.doc"
FileCopy File1, File2
Debug.Print File1 & " "; FileDateTime(File1)
Debug.Print File2 & " "; FileDateTime(File2)
End Sub
' C:\Junk.doc 2/14/2006 7:52:30 PM
' 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
-
Feb 15th, 2006, 04:45 PM
#3
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:
Option Explicit
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 SetFileTime Lib "kernel32" (ByVal hFile As Long, _
lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, _
lpFileTime As FILETIME) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, _
lpFileTime As FILETIME) As Long
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Sub ChangeFileTime(FilePath As String, NewDateModified As Date)
Dim lngHandle As Long
Dim udtFileTime As FILETIME
Dim udtLocalTime As FILETIME
Dim udtSystemTime As SYSTEMTIME
With udtSystemTime
.wYear = Year(NewDateModified)
.wMonth = Month(NewDateModified)
.wDay = Day(NewDateModified)
.wDayOfWeek = Weekday(NewDateModified) - 1
.wHour = Hour(NewDateModified)
.wMinute = Minute(NewDateModified)
.wSecond = Second(NewDateModified)
.wMilliseconds = 0
End With
' convert system time to local time
SystemTimeToFileTime udtSystemTime, udtLocalTime
' convert local time to GMT
LocalFileTimeToFileTime udtLocalTime, udtFileTime
' open the file to get the filehandle
lngHandle = CreateFile(FilePath, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, OPEN_EXISTING, 0, 0)
' change date/time property of the file
SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
' close the handle
CloseHandle lngHandle
End Sub
Private Sub CopyFile(FromPath As String, ToPath As String)
Dim TempDateTime As Date
TempDateTime = FileDateTime(FromPath)
FileCopy FromPath, ToPath
ChangeFileTime ToPath, TempDateTime
End Sub
Then call it like this..
VB Code:
Private Sub Command1_Click()
Debug.Print FileDateTime("C:\Junk.doc") 'check the file time before copying
CopyFile "C:\Junk.doc", "E:\Junk.doc"
Debug.Print FileDateTime("E:\Junk.doc") 'check the copied file time, should be the same
End Sub
Last edited by jcis; Feb 15th, 2006 at 04:49 PM.
-
Feb 16th, 2006, 09:49 AM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|