How to get create/last modified dates of a file in Delphi?

25,844

Solution 1

Delphians tend to like the FindFirst approach (the SearchRec structure has some of those), but I'd suggest the Win32 API function GetFileAttributesEx.

Solution 2

Try

function FileAge(const FileName: string; out FileDateTime: TDateTime): Boolean;

From SysUtils.

Solution 3

From the DSiWin32 freeware library:

function DSiFileTimeToDateTime(fileTime: TFileTime; var dateTime: TDateTime): boolean;
var
  sysTime: TSystemTime;
begin
  Result := FileTimeToSystemTime(fileTime, sysTime);
  if Result then
    dateTime := SystemTimeToDateTime(sysTime);
end; { DSiFileTimeToDateTime }

function  DSiGetFileTimes(const fileName: string; var creationTime, lastAccessTime,
  lastModificationTime: TDateTime): boolean; 
var
  fileHandle            : cardinal;
  fsCreationTime        : TFileTime;
  fsLastAccessTime      : TFileTime;
  fsLastModificationTime: TFileTime;
begin
  Result := false;
  fileHandle := CreateFile(PChar(fileName), GENERIC_READ, FILE_SHARE_READ, nil,
    OPEN_EXISTING, 0, 0);
  if fileHandle <> INVALID_HANDLE_VALUE then try
    Result :=
      GetFileTime(fileHandle, @fsCreationTime, @fsLastAccessTime,
         @fsLastModificationTime) and
      DSiFileTimeToDateTime(fsCreationTime, creationTime) and
      DSiFileTimeToDateTime(fsLastAccessTime, lastAccessTime) and
      DSiFileTimeToDateTime(fsLastModificationTime, lastModificationTime);
  finally
    CloseHandle(fileHandle);
  end;
end; { DSiGetFileTimes }

Solution 4

function GetFileModDate(filename : string) : TDateTime;
var
   F : TSearchRec;
begin
   FindFirst(filename,faAnyFile,F);
   Result := F.TimeStamp;
   //if you really wanted an Int, change the return type and use this line:
   //Result := F.Time;
   FindClose(F);
end;

F.Time has since been Deprecated, Help file says Use F.TimeStamp.
Just to update this due to later versions of Delphi

Solution 5

This should work, and it is native Delphi code.

function GetFileModDate(filename : string) : integer;
var
  F : TSearchRec;
begin
  FindFirst(filename,faAnyFile,F);
  Result := F.Time;
  //if you wanted a TDateTime, change the return type and use this line:
  //Result := FileDateToDatetime(F.Time);
  FindClose(F);
end;
Share:
25,844
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to get a files these attributes as integer values.

  • Kohi
    Kohi almost 12 years
    why var rather than out parameter - you don't seem to use the value that comes in?
  • med
    med almost 12 years
    Valid point. I'm an 'old school' programmer, I learned it before 'out' was invented and I automatically use 'var' in all occasions.
  • HX_unbanned
    HX_unbanned over 11 years
    Deprecated as of time of this comment.
  • Jerry Gagnon
    Jerry Gagnon almost 11 years
    FileAge() gets the CREATE date of the file, not the LAST MODIFIED date.
  • Gerry Coll
    Gerry Coll almost 8 years
    @JerryGagnon - No, it uses _WIN32_FILE_ATTRIBUTE_DATA.ftLastWriteTime
  • Gerry Coll
    Gerry Coll almost 8 years
    @HX_unbanned - only the first overload (returning a FileTime) is deprecated
  • dummzeuch
    dummzeuch over 4 years
    That overload was added in Delphi 2006 (in case somebody else runs into a compile error with older versions)