[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

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

0
PeterdeB
Asked:
PeterdeB
1 Solution
 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now