• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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.
0
RRR
Asked:
RRR
  • 3
  • 3
  • 2
  • +3
1 Solution
 
rkot2000Commented:
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.
0
 
RRRAuthor Commented:
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
0
 
GrahamAtJobserveCommented:
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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
VincentLawlorCommented:
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.
0
 
RRRAuthor Commented:
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.
0
 
GrahamAtJobserveCommented:
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.
0
 
VincentLawlorCommented:
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.
0
 
rkot2000Commented:
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.
0
 
PaulHewsCommented:
>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)

Not true, you will only get ambiguity if you use built in date formats such as "short date."  There is no API to get a unique ID from a date, although if you do this:

dim MyDate as Date
MyDate = Now
debug.print cdbl(MyDate)

You will see how date variables are stored internally.
0
 
rkot2000Commented:
>>>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.
0
 
ArkCommented:
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
0
 
RRRAuthor Commented:
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now