Get directory creation date as TDateTime

284 Views Asked by At

I'm wondering, how can I get a folder's creation date as a TDateTime?

FileAge() does not work with folders, only files, so sadly datetime := FileDateToDateTime(FileAge()) won't work for me.

Any help would be greatly appreciated as I am a bit stumped here.

2

There are 2 best solutions below

2
Remy Lebeau On

The version of FileAge() you are calling is deprecated (the compiler should be warning you about that). You should be using the newer overload that has a TDateTime output parameter:

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

For example:

if FileAge(path, datetime) then
begin
  // use datetime ...
end;

In any case, both versions of FileAge() use the Win32 GetFileAttributesEx() and/or FindFirstFile() APIs internally, which both work just fine with folders, it is just that FileAge() filters the output to ignore folders.

So, just use those Win32 APIs directly instead. Or, you can use the RTL's FileGetDateTimeInfo() function instead:

function FileGetDateTimeInfo(const FileName: string; out DateTime: TDateTimeInfoRec; FollowLink: Boolean = True): Boolean;

TDateTimeInfoRec has a CreationTime property of type TDateTime.

For example:

var
  info: TDateTimeInfoRec;

if FileGetDateTimeInfo(path, info) then
begin
  datetime := info.CreationTime;
  // use datetime ...
end;
3
Stefan Glienke On