How to compare file last modified dates?

Hi Folks,

I attached some code snippet which reads the last modified date from two files into a string. Then with a buttonclick I d like to compare and determine which one is older, but that is where it goes wrong.

Can anyone help me out?

Regards,

Peter
function FileLastModified
  (const TheFile: string): string;
var
 FileH            : THandle;
 LocalFT          : TFileTime;
 DosFT            : DWORD;
 LastAccessedTime : TDateTime;
 FindData         : TWin32FindData;
begin
 Result := '';
 FileH := FindFirstFile(PChar(TheFile), FindData);
 if FileH <> INVALID_HANDLE_VALUE then begin
  Windows.FindClose(FileH);
  if (FindData.dwFileAttributes AND
      FILE_ATTRIBUTE_DIRECTORY) = 0 then
   begin
    FileTimeToLocalFileTime
     (FindData.ftLastWriteTime,LocalFT);
    FileTimeToDosDateTime
     (LocalFT,LongRec(DosFT).Hi,LongRec(DosFT).Lo);
    LastAccessedTime := FileDateToDateTime(DosFT);
    Result := DateTimeToStr(LastAccessedTime);
   end;
 end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
 edit1.text := FileLastModified('c:\backup\database.mdb');
    edit2.text := FileLastModified('c:\database.mdb');
end;
 
 
procedure TForm1.Button1Click(Sender: TObject);
var
  date1, date2:string;
begin
  //dd.mm.yyyy (hh:nn)
  date1:=edit1.text;
  date2:=edit2.text;
  if (StrToDateTime(Copy(date1, 4, 2)+
                Copy(date1, 1, 2)+
                Copy(date1, 7, 4)+' '+
                Copy(date1, 13, 5))
      )>
      (StrToDateTime(Copy(date2, 4, 2)+
                Copy(date2, 1, 2)+
                Copy(date2, 7, 4)+' '+
                Copy(date2, 13, 5))
      ) then
    ShowMessage(date1+' is greater than '+date2)
  end;

Open in new window

PeterdeBAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JosephGloszCommented:
Well, in the first place, I'd change the function result to TDateTime. Why convert it to a string, then convert the string back to a TDateTime for comparisions? You could still populate your edit boxes separately if you want.

function FileLastModified   (const TheFile: string): TDateTime;

Then, in your buttonclick, I'd change it to follow this form:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if FileLastModified('A') > FileLastModified('B') then ShowMessage('Msg');
end;

Keep in mind that on some OSs, the time stamp on a file has only a two second resolution.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.