Search results for 'FileTimeToDateTime'. 1 post(s) found.

  1. 2009/08/19 How to get file created time, modified time, and last accessed time ?
2009/08/19 14:46

How to get file created time, modified time, and last accessed time ?


In order to get the File Attributes, you can get it by file search API.

Here's an example of how to show the creation, last access and last modification dates and times of a file:

uses SysUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
SR: TSearchRec;
CreateDT, AccessDT, ModifyDT: TDateTime;
begin
if FindFirst('c:\test\text01.txt', faAnyFile, SR) = 0 then begin
CreateDT := FileTimeToDateTime(SR.FindData.ftCreationTime);
AccessDT := FileTimeToDateTime(SR.FindData.ftLastAccessTime);;
ModifyDT := FileTimeToDateTime(SR.FindData.ftLastWriteTime);;
ShowMessage('Created: ' + DateTimeToStr(CreateDT) +
' Accessed: ' + DateTimeToStr(AccessDT) +
' Modified: ' + DateTimeToStr(ModifyDT));
end
else
ShowMessage('Sorry, could not find the file');
  FindClose(SR);
end;
In delphi 2009, you need to call FirndFirst functions as SysUtils.FindFirst. So you need to use as below way in Delphi 2009.
procedure TForm1.Button1Click(Sender: TObject);
var
SR: TSearchRec;
CreateDT, AccessDT, ModifyDT: TDateTime;
begin
if SysUtils.FindFirst('c:\test\text01.txt', faAnyFile, SR) = 0 then begin
CreateDT := FileTimeToDateTime(SR.FindData.ftCreationTime);
AccessDT := FileTimeToDateTime(SR.FindData.ftLastAccessTime);;
ModifyDT := FileTimeToDateTime(SR.FindData.ftLastWriteTime);;
ShowMessage('Created: ' + DateTimeToStr(CreateDT) +
' Accessed: ' + DateTimeToStr(AccessDT) +
' Modified: ' + DateTimeToStr(ModifyDT));
end
else
ShowMessage('Sorry, could not find the file');
  FindClose(SR);
end;
In addition, if your delphi does not support FileTimeToDateTime function use below function.
function FileTimeToDateTime( FileTime : TFileTime ) : TDateTime;
var
ModifiedTime : TFileTime;
SystemTime : TSystemTime;
begin
try
FileTimeToLocalFileTime( FileTime, ModifiedTime );
FileTimeToSystemTime( ModifiedTime, SystemTime );
Result := SystemTimeToDateTime( SystemTime );
except
Result := Now;
end;
end;

Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.