...get the last access from a file?
Author: Simon Grossenbacher
{1.}
function GetFileLastAccessTime(sFileName: string): TDateTime;
var
ffd: TWin32FindData;
dft: DWORD;
lft: TFileTime;
h: THandle;
begin
//
// get file information
h := Windows.FindFirstFile(PChar(sFileName), ffd);
if (INVALID_HANDLE_VALUE <> h) then
begin
//
// we're looking for just one file,
// so close our "find"
Windows.FindClose(h);
//
// convert the FILETIME to
// local FILETIME
FileTimeToLocalFileTime(ffd.ftLastAccessTime, lft);
//
// convert FILETIME to
// DOS time
FileTimeToDosDateTime(lft, LongRec(dft).Hi, LongRec(dft).Lo);
//
// finally, convert DOS time to
// TDateTime for use in Delphi's
// native date/time functions
Result := FileDateToDateTime(dft);
end;
end;
{********************************************************************}
{2.}
function GetFileTimes(const FileName: string; var Created: TDateTime;
var Accessed: TDateTime; var Modified: TDateTime): Boolean;
var
h: THandle;
Info1, Info2, Info3: TFileTime;
SysTimeStruct: SYSTEMTIME;
TimeZoneInfo: TTimeZoneInformation;
Bias: Double;
begin
Result := False;
Bias := 0;
h := FileOpen(FileName, fmOpenRead or fmShareDenyNone);
if h > 0 then
begin
try
if GetTimeZoneInformation(TimeZoneInfo) <> $FFFFFFFF then
Bias := TimeZoneInfo.Bias / 1440; // 60x24
GetFileTime(h, @Info1, @Info2, @Info3);
if FileTimeToSystemTime(Info1, SysTimeStruct) then
Created := SystemTimeToDateTime(SysTimeStruct) - Bias;
if FileTimeToSystemTime(Info2, SysTimeStruct) then
Accessed := SystemTimeToDateTime(SysTimeStruct) - Bias;
if FileTimeToSystemTime(Info3, SysTimeStruct) then
Modified := SystemTimeToDateTime(SysTimeStruct) - Bias;
Result := True;
finally
FileClose(h);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Date1, Date2, Date3: TDateTime;
begin
if GetFileTimes(Edit1.Text, Date1, Date2, Date3) then
begin
ShowMessage('Created: ' + DateTimeToStr(Date1));
ShowMessage('Last Accessed: ' + DateTimeToStr(Date2));
ShowMessage('Last Modified: ' + DateTimeToStr(Date3));
end;
end;
printed from
www.swissdelphicenter.ch
developers knowledge base