Solved

LastModifiedDate compare between 2 files.

Posted on 2001-09-04
12
203 Views
Last Modified: 2008-02-01
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
Comment
Question by:RRR
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
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
 
LVL 3

Author Comment

by:RRR
Comment Utility
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
 

Expert Comment

by:GrahamAtJobserve
Comment Utility
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
 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
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
 
LVL 3

Author Comment

by:RRR
Comment Utility
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
 

Expert Comment

by:GrahamAtJobserve
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 4

Expert Comment

by:VincentLawlor
Comment Utility
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
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
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
 
LVL 38

Accepted Solution

by:
PaulHews earned 300 total points
Comment Utility
>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
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
>>>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
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
 
LVL 3

Author Comment

by:RRR
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now