Re: file attributes: read creation and last access dates
Posted by webmaster Guido on September 08, 2003
In Reply to: Delphi: file attributes: read creation and last access dates posted by Stefan Loeners on September 07, 2003:
: How would I retrieve the CREATION date and the LAST ACCESS date of a given file?
Fat32 & NTFS are the only file systems that support creation and last access date/time, other file systems only support last modification date/time. Maybe that's why there are no functions to get the most recent access and update date/time, but we can calculate them using Delphi's FindFirst function.
FindFirst fills a TSearchRec record with info about a particular file. One of the fields of the TSearchRec is FindData.
Among other info, FindData has has 3 fields with date/time info of the file's creation, last access and last modification: ftCreationTime, ftLastAccessTime and ftLastWriteTime. These 3 fields are declared as TFileTime, a type that represents 64-bit dates in the Coordinated Universal Time format (UTC). To convert TFileTime to Delphi's TDateTime, you can use the following function:
function FileTimeToDTime(FTime: TFileTime): TDateTime; var LocalFTime: TFileTime; STime: TSystemTime; begin FileTimeToLocalFileTime(FTime, LocalFTime); FileTimeToSystemTime(LocalFTime, STime); Result := SystemTimeToDateTime(STime); end;
Here's an example of how to show the creation, last access and last modification dates and times of a file:
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 := FileTimeToDTime(SR.FindData.ftCreationTime); AccessDT := FileTimeToDTime(SR.FindData.ftLastAccessTime);; ModifyDT := FileTimeToDTime(SR.FindData.ftLastWriteTime);; ShowMessage('Created: ' + DateTimeToStr(CreateDT) + ' Accessed: ' + DateTimeToStr(AccessDT) + ' Modified: ' + DateTimeToStr(ModifyDT)); end else ShowMessage('Sorry, could not find the file');
[ DelphiLand: free Delphi source code, tips, tutorials ]