2010. augusztus 19., csütörtök

Get the date a file was created


Problem/Question/Abstract:

How to get the date a file was created

Answer:

Solve 1:

uses
  Windows, Systutils;

function GetFileCreateDate(TheFile: string): TDateTime;
var
  SearchRec: TSearchRec;
  DT: TFileTime;
  ST: TSystemTime;
begin
  Result := 0;
  try
    if (FindFirst(TheFile, faAnyFile, SearchRec) = 0) then
    begin
      FileTimeToLocalFileTime(SearchRec.FindData.ftCreationTime, DT);
      FileTimeToSystemTime(DT, ST);
      Result := EncodeDate(st.wYear, st.wMonth, st.wDay) +
        EncodeTime(st.wHour, st.wMinute, st.wSecond, 0);
    end;
  finally
    FindClose(SearchRec);
  end;
end;


Solve 2:

{This function returns the file creation timestamp of the specified file.
Uses-clause order dependency: if "Windows" is used, it must come before "SysUtils".}

function GetFileCreationTimestamp(const FileName: string): TDateTime;
var
  SearchRec: TSearchRec;
begin
  if (FindFirst(Filename, faAnyfile, SearchRec) = 0) then
  begin
    FindClose(SearchRec);
    if ((SearchRec.FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0) then
      Result := UTCFileTimeToLocalTimestamp(SearchRec.FindData.ftCreationTime)
    else
      raise Exception.Create('GetFileCreationTimestamp: File is a directory.');
  end
  else
    raise Exception.Create('GetFileCreationTimestamp: File does not exist.');
end;

{This function converts a TFileTime in UTC to a TDateTime for the local time zone.}

function UTCFileTimeToLocalTimestamp(const UTCFileTime: TFileTime): TDateTime;
var
  LocalFileTime: TFileTime;
  FATDateTime: LongInt;
begin
  if (FileTimeToLocalFileTime(UTCFileTime, LocalFileTime)) then
    if (FileTimeToDosDateTime(LocalFileTime, LongRec(FATDateTime).Hi,
      LongRec(FATDateTime).Lo)) then
      Result := FileDateToDateTime(FATDateTime)
    else
      raise
        Exception.Create('UTCFileTimeToLocalTimestamp: Timestamp conversion error.')
  else
    raise Exception.Create('UTCFileTimeToLocalTimestamp: Timestamp conversion error.')
end;


Solve 3:

function FileTimeToLocalDateTime(filetime: TFileTime): TDatetime;
var
  LocalFileTime: TFileTime;
  dostime: Longint;
begin
  FileTimeToLocalFileTime(filetime, LocalFileTime);
  if FileTimeToDosDateTime(LocalFileTime, LongRec(dostime).Hi, LongRec(dostime).Lo)
    then
    Result := FiledateToDatetime(dostime)
  else
    Result := 0.0;
end;

procedure GetFileTimes(filename: string; var creationtime, lastaccesstime,
  lastwritetime: TDateTime);
var
  srec: TSearchRec;
begin
  if FindFirst(filename, faAnyfile, Srec) = 0 then
  try
    with SRec.FindData do
    begin
      creationtime := FileTimeToLocalDateTime(ftCreationTime);
      lastaccesstime := FileTimeToLocalDateTime(ftLastAccessTime);
      lastwritetime := FileTimeToLocalDateTime(ftLastWriteTime);
    end;
  finally
    FindClose(SRec);
  end
  else
    raise Exception.Create('File %s not found!', [filename]);
end;

You get from a TDatetime to string via Format, FormatDatetime, DateTimeToStr etc..

Nincsenek megjegyzések:

Megjegyzés küldése