2004. május 17., hétfő

Retrieve a file's "Last Accessed" attribute


Problem/Question/Abstract:

Retrieve a file's "Last Accessed" attribute

Answer:

In Windows 95, you can see when a file was last accessed by right-clicking the file and selecting properties. You can retrieve this date easily with the following ready-to-use function:

function LastAccess(const filename: string): string;
var
  FileHandle: THandle;
  LocalFileTime: TFileTime;
  DosFileTime: DWORD;
  LastAccessedTime: TDateTime;
  FindData: TWin32FindData;
begin
  Result := ''; { never :-) }
  FileHandle := FindFirstFile(filename, FindData);
  if FileHandle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      FileTimeToDosDateTime(LocalFileTime,
        LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo);
      LastAccessedTime := FileDateToDateTime(DosFileTime);
      Result := DateTimeToStr(LastAccessedTime);
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése