Link to home
Start Free TrialLog in
Avatar of RRR
RRRFlag for Israel

asked on

LastModifiedDate compare between 2 files.

Hi, Experts.
I need to compare the last modified date of 2 files (2 same files on different computers).
The problem is on 1 computer the last modified date is 4/9/2001 2:12:24 PM and on the 2 computer this date is 9/4/2001 2:13:15 PM. My program should change the older file with the new one. But on checking the dates it doesn't do it, because it think that the current file is the newer.(The comparing that I do is between two strings.)
I need to check the system identificator of both last modified dates. I need the API function that will convert this date to the Identificator.

Please, I need it ASAP.
Thanks, RRR.
Avatar of rkot2000
rkot2000

can you elaborate you question (system identificator)

Anyway you can try to use this api to get file info

Declare Function GetFileTime& Lib "kernel32" (ByVal hFile As Long, _
lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As _
FILETIME)


hFile     Long?A handle to a file.
lpCreationTime     FILETIME?A FILETIME structure to load with the creation time of the file.

lpLastAccessTime     FILETIME?A FILETIME structure to load with the last access time of the file (not supported by FAT file systems).

lpLastWriteTime     FILETIME?A FILETIME structure to load with the last modification time of the file.
Avatar of RRR

ASKER

Hi rkot2000,

The problem is not to get 'Last modified date' attribute of the file, but comparing 2 strings that represent dates
but on 2 different computers with unknown regional date and time settings.
Therefore I need an API function that converts a date to a unique value (Identificator) that can be compared on the remote computer with the remote date unique value.

thaks RRR
You dont need an api function.. you just need to do some string manipulation.

Convert the 2 dates into strings of the form yyyymmddhhnnss
and then compare string 1 with string 2. The bigger is the most recent.

Try this

Dim fso
    Dim fil1, fil2
   
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    Set fil1 = fso.GetFile("C:\Test1.txt")
    Set fil2 = fso.GetFile("C:\Test2.txt")
   
    Dim dtFile1 As Date
    dim dtFile2 as Date
   
    dtFile1 = fil1.DateLastModified
    dtFile2 = fil2.DateLastModified
   
    Dim lYears As Long
    Dim lMonths As Long
    Dim lDays As Long
    Dim lHours As Long
    Dim lMins As Long
    Dim lSecs As Long
   
    lYears = DateDiff("yyyy", dtFile1, dtFile2)
    lMonths = DateDiff("m", dtFile1, dtFile2)
    lDays = DateDiff("d", dtFile1, dtFile2)
    lHours = DateDiff("h", dtFile1, dtFile2)
    lMins = DateDiff("n", dtFile1, dtFile2)
    lSecs = DateDiff("s", dtFile1, dtFile2)

Vin.
Avatar of RRR

ASKER

Hi, GrahamAtJobserve.
I had this solution in different way, because your way will do some problems( you can not know where is the day and month of the date). I solved it by using day(mydate) & Month(mydate) & year(mydate) & format(mydate,"HH:NN:SS AMPM") functions.
BUT I would like to know the API function that will return me the unique Identificator of this date.

Thanks, RRR.
Using vincents code

if dtFile1 > dtFile2 then
   msgbox "File 1 is most recent"
else
   msgbox "File 2 is most recent"
end if

as dtFile1 and dtFile2 are both dates, you can safely compare them.
I was just about to post that as an afterthought

But you get the idea. This should solve your problem for you. Same code as above

    If dtFile1 = dtFile2 Then
        'Files modified at the same time
    ElseIf dtFile1 > dtFile2 Then
        'File1 modified after File2
    ElseIf dtFile1 < dtFile2 Then
        'File2 modified after File1
    End If

You can also use the functions

   dtFile1 = fil1.DateCreated
   dtFile1 = fil1.DateLastAccessed
   
For your comparisons.

Hope that sorts it out.

Vin.
How about Coordinated Universal Time(UTC, also known as GMT).

File times returned by this function are UTC.


Type FILETIME  '  8  Bytes
     dwLowDateTime As Long
     dwHighDateTime As Long
End Type

Description

64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.

Field     Type/Description
dwLowDateTime, dwHighDateTime     Long?Low and high-order 32 bits of the file time.

Plus you can convert to local time

FileTimeToLocalFileTime

VB Declaration

Declare Function FileTimeToLocalFileTime& Lib "kernel32" (lpFileTime As _
FILETIME, lpLocalFileTime As FILETIME)

Description

Converts a FILETIME structure to local time.

Use with VB

No problem.

Parameter     Type/Description
lpFileTime     FILETIME?Structure containing time in UTC.
lpLocalFileTime     FILETIME?Structure to load with the converted local time.
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>Therefore I need an API function that converts a date to a unique value (Identificator) that can be
compared on the remote computer with the remote date unique value.



How about Coordinated Universal Time(UTC, also known as GMT).

File times returned by this function(GetFileTime) are UTC.

Type FILETIME  '  8  Bytes
     dwLowDateTime As Long
     dwHighDateTime As Long
End Type

Description

64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.

Field     Type/Description
dwLowDateTime, dwHighDateTime     Long?Low and high-order 32 bits of the file time.

Plus you can convert UTC time to local time :

FileTimeToLocalFileTime

VB Declaration

Declare Function FileTimeToLocalFileTime& Lib "kernel32" (lpFileTime As _
FILETIME, lpLocalFileTime As FILETIME)

Description

Converts a FILETIME structure to local time.

Use with VB

No problem.

Parameter     Type/Description
lpFileTime     FILETIME?Structure containing time in UTC.
lpLocalFileTime     FILETIME?Structure to load with the converted local time.
Avatar of Ark
Hi
Creating, modifying etc dates store internally in FILE_TIME format (see rkot2000 messages) as LongIntegers (64 bit), not depending to System locale date setting. So, FILE_TIME structure already is "identificator" you're looking for.

To get FileTime structure, you can use GetFileTime API, as rkot2000 showed in his first message.
Then you can compare them:

Declare Function CompareFileTime& Lib "kernel32" (lpFileTime1 As FILETIME, _
lpFileTime2 As FILETIME)

n = CompareFileTime(FT1,FT2)

Return Value

Long?0 if the two times are equal. ?1 if lpFileTime1 is less than lpFileTime2. 1 if lpFileTime1 is more than lpFileTime2.

If you need to convert System Locale Time to "identificator", use

Type SYSTEMTIME  '  16  Bytes
     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


Declare Function SystemTimeToFileTime& Lib "kernel32" (lpSystemTime As _
SYSTEMTIME, lpFileTime As FILETIME)


Cheers
Avatar of RRR

ASKER

Hi, All.
I accept the PaulHews's comments as answer, because it is the best solution for me, and it is the simplest and most effective one.

Thanks for all of you,
RRR.