windows file dates inherently unreliable?

windows file dates inherently unreliable?

Anonymous
Not applicable
425 Views
5 Replies
Message 1 of 6

windows file dates inherently unreliable?

Anonymous
Not applicable
Hi,
looking at file dates with win api
below is a sample printout at end of function after reading a directory of
files:

C:\vb\z.bas
Created: 9/5/2003 15:4:20:981
Last Accessed: 9/5/2003 15:4:20:991
Last saved: 1/30/2003 3:1:52:0
Done files...
Out of 822 files, there were 731 with saved date before created date
That took: 0.24 seconds

why would created date be later than last saved date?
if created date is unreliable is saved date at least accurate?
anyone can shed some light on this?
tia
mark
0 Likes
426 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
I've seen this before and whilst I've never seen confirmation anywhere, I deduced myself that it can occur when you copy a file, So if you modify z.bas on January 30th and then copy it to another directory on September 5th I think the new file would exhibit these properties.



Regards



Wayne Ivory

IT Analyst Programmer

Wespine Industries Pty Ltd
0 Likes
Message 3 of 6

Anonymous
Not applicable
Please post the code you used to generate the information.

Joe
--
0 Likes
Message 4 of 6

Anonymous
Not applicable
"joesu" wrote in message
news:f19de7f.1@WebX.maYIadrTaRb...
> Please post the code you used to generate the information.
> Joe
> --
>

i think this is most of it,
some is in a class mod cmnFile.cls
the calling sub is in a test module.
(some of the declares are for other parts of the class, not all are used in
this example)
'**** this stuff in cls mod
'adapted from example by:
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@allapi.net
'as well as info gleaned from msdn
'compare file times
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 Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
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
'declare winapi functions
Private Declare Function SearchTreeForFile Lib "IMAGEHLP.DLL" (ByVal
lpRootPath As String, ByVal lpInputName As String, ByVal lpOutputName As
String) As Long
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 Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long,
lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As
FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As
FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime
As FILETIME, lpLocalFileTime As FILETIME) As Long

Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime
As SYSTEMTIME, lpFileTime As FILETIME) As Long
Private Declare Function CompareFileTime Lib "kernel32" (lpFileTime1 As
FILETIME, lpFileTime2 As FILETIME) As Long

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA"
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA"
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias
"GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long)
As Long

Public Sub GetFileTimes(ByVal sFileName As String, ByRef colRtn As
Collection)
'file handle
Dim lngFhndl As Long
'file times
Dim ftCreated As FILETIME
Dim ftLastAccess As FILETIME
Dim ftSaved As FILETIME
'local time
Dim ftlCreated As FILETIME
Dim ftlLastAccess As FILETIME
Dim ftlSaved As FILETIME
'system time
Dim ftsCreated As SYSTEMTIME
Dim ftsLastAccess As SYSTEMTIME
Dim ftsSaved As SYSTEMTIME

'get handle
lngFhndl = GetFileHandle(sFileName)
'fill in filetime structures
GetFileTime lngFhndl, ftCreated, ftLastAccess, ftSaved
'close handle
CloseFile (lngFhndl)

'Convert the file times to the local file time
FileTimeToLocalFileTime ftCreated, ftlCreated
FileTimeToLocalFileTime ftLastAccess, ftlLastAccess
FileTimeToLocalFileTime ftSaved, ftlSaved

'Convert the file times to system file time
FileTimeToSystemTime ftlCreated, ftsCreated
FileTimeToSystemTime ftlLastAccess, ftsLastAccess
FileTimeToSystemTime ftlSaved, ftsSaved

'cant pass a systemtime structure so convert to string
Dim sCreated As String
Dim sAccessed As String
Dim sSaved As String

'add strings to byref return collection argument/parameter
colRtn.Add GetTimeString(ftsCreated)
colRtn.Add GetTimeString(ftsLastAccess)
colRtn.Add GetTimeString(ftsSaved)

End Sub
Private Function GetTimeString(ByRef sFileTime As SYSTEMTIME) As String
Dim sTime As String
sTime = Str$(sFileTime.wMonth) _
+ "/" + LTrim(Str$(sFileTime.wDay)) _
+ "/" + LTrim(Str$(sFileTime.wYear)) _
+ " " + LTrim(Str$(sFileTime.wHour)) _
+ ":" + LTrim(Str$(sFileTime.wMinute)) _
+ ":" + LTrim(Str$(sFileTime.wSecond)) _
+ ":" + LTrim(Str$(sFileTime.wMilliseconds))
GetTimeString = sTime
End Function


Private Function GetFileHandle(ByVal sFileName As String) As Long
Dim hFile As Long
'create a handle to the file
hFile = CreateFile(sFileName, 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal
0&)
'return file handle
GetFileHandle = hFile
End Function
'******end of cls mod

'*********this in test module
Sub testgetfiles()

Dim cd As cmnDebug
Set cd = New cmnDebug
cd.DebugOn = True

Dim cF As cmnFile
Set cF = New cmnFile

Dim sDirA As String
sDirA = "C:\0\0code\vb\"

Dim vRtn As Variant
'get list of files
vRtn = cF.GetFilesAPI(sDirA, "*")

Dim lb As Integer
Dim ub As Integer
lb = LBound(vRtn)
ub = UBound(vRtn)
Dim iNumfiles As Integer
iNumfiles = ub - lb + 1
Dim icount As Integer
icount = 0

Dim i As Integer
Dim sCreatedA As String
Dim sAccessedA As String
Dim sSavedA As String

Dim j As Integer

For i = lb To ub
'fresh collection ea time
Dim colRes As New Collection
cF.GetFileTimes sDirA & vRtn(i), colRes
sCreatedA = colRes(1)
sAccessedA = colRes(2)
sSavedA = colRes(3)
If sCreatedA > sSavedA Then
icount = icount + 1
End If
'clear collection
Set colRes = Nothing
Next
cd.dp sDirA & vRtn(i - 1)
cd.dp "Created: " & sCreatedA
cd.dp "Last Accessed: " & sAccessedA
cd.dp "Last saved: " & sSavedA
cd.dp "out of " & iNumfiles & " files, there were " & icount & " with
saved date before created date"

End Sub

cd.dp is basically debug.print with a boolean switch for on or off.
(in another .cls)
fwiw I think wayne is right about the copying of a file updating it's
created date, but not saved date. which explains my results.
thanks all
Mark
0 Likes
Message 5 of 6

Anonymous
Not applicable
It does have some peverse logic when you think about it. The new file was created when you copied it, but it was last modified when you modified the original.



Wayne
0 Likes
Message 6 of 6

Anonymous
Not applicable
Yeah,
I'm okay with it now, I really didn't care about the create date - I was
just surprised when it was coming up after the save date and it started me
worrying whether I could trust the save date, cause I'm wanting to use this
for backing up/ syncing my working files and will be depending on the
reported save times to decide which files get overwritten/updated
like you say, it does make sense once I realized they were copies of older
files.
Thanks again
Mark

"wivory" wrote in message
news:f19de7f.3@WebX.maYIadrTaRb...
> It does have some peverse logic when you think about it. The new file was
created when you copied it, but it was last modified when you modified the
original.
>
> Wayne
0 Likes