Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Folder creation datetime (https://www.delphipraxis.net/173977-folder-creation-datetime.html)

WojTec 26. Mär 2013 18:51

Folder creation datetime
 
Delphi-Quellcode:
function GetFolderDate(const AFolder: string): TDateTime;
var
  FolderData: TWin32FileAttributeData;
  FileTime: _FILETIME;
//  FileTime: _SYSTEMTIME;
  DW: DWORD;
begin
  Result := 0;
  FillChar(FolderData, SizeOf(FolderData), 0);

  if GetFileAttributesEx(PChar(AFolder), GetFileExInfoStandard, @FolderData) then
  begin
    FileTimeToLocalFileTime(FolderData.ftCreationTime, FileTime);
    FileTimeToDosDateTime(FileTime, LongRec(DW).Hi, LongRec(DW).Lo);
    Result := FileDateToDateTime(DW);

//    FileTimeToSystemTime(FolderData.ftCreationTime, FileTime);
//    Result:= SystemTimeToDateTime(FileTime);
  end;
end;
So, both actual and commented code returns invalid creation. What is wrong? :?

ASM 26. Mär 2013 20:13

AW: Folder creation datetime
 
Did you try to parameterize the function DateTimeToStr() by including a variable of type TFormatSettings ?,
e.g.:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
Var tf: TFormatSettings;
begin
 GetLocaleFormatSettings(GetSystemDefaultLCID, tf);
 Showmessage(DateTimeToStr(GetFolderDate('c:\Windows'),tf));
end;
If that will fail, then are you absolutely shure calling for a time stamp of a truly existing foldername ? Curiously enough, if you are calling GetFolderDate() for an empty or nonexisting or misspelled foldername that will respond with a time stamp corresponding to '30.12.1899'.

WojTec 26. Mär 2013 20:26

Re: Folder creation datetime
 
So, problem is with conversion system time to Delphis time. :cyclops:

The same conversion method I used to get file properties and was oke, but for folders is not oke... :?

Volker Z. 27. Mär 2013 02:37

AW: Folder creation datetime
 
Hi,

maybe the following code could be some help for you - feel free to use it, if you like
Delphi-Quellcode:
type
  TFileTime = (ftAccess, ftCreated, ftWrite);

function GetFileDateTime (const Filename : string; const Filetime : TFileTime) : TDateTime;
var
  f : TSearchRec;
  t : _FILETIME;
  s : _SYSTEMTIME;
  d : Integer;

  function GetTimeDiff : Integer;
  var
    t0, t1 : _SYSTEMTIME;
  begin
    GetLocalTime (t0);
    GetSystemTime (t1);

    Result := t0.wHour - t1.wHour;
  end;

begin
  Result := 0;

  try
    if FindFirst (Filename, faAnyFile, f) <> 0 then
      raise Exception.Create ('File ' + Filename + ' not found');

    case Filetime of
      ftAccess : t := f.FindData.ftLastAccessTime;
      ftCreated : t := f.FindData.ftCreationTime;
      ftWrite  : t := f.FindData.ftLastWriteTime;
      else
        raise Exception.Create ('Invalid type of file time')
    end;

    if not FileTimeToSystemTime (t, s) then
      raise Exception.Create ('Could not convert file time to system time format');

    d := GetTimeDiff;
    with s do
      Result := EncodeDate (wYear, wMonth, wDay) + EncodeTime (wHour + d, wMinute, wSecond, wMilliseconds);

    Result := Result
  finally
    FindClose (f)
  end
end;
A call like
Delphi-Quellcode:
ShowMessage (FormatDateTime ('dddd, d. mmmm yyyy hh:nn:ss', GetFileDateTime ('any file or folder you address here', ftCreated)))
should produce a message box showing the files / folders creation date.

Have a nice day

Bummi 27. Mär 2013 06:33

AW: Folder creation datetime
 
@Volker

Deine Routine GetTimeDiff dürfte mitunter unerwartete Ergebnisse liefern ... bei uns zumindest um die Geisterstunde herum ...

Bummi 27. Mär 2013 18:24

AW: Folder creation datetime
 
Delphi-Quellcode:
function GetFolderDate(const AFolder: string): TDateTime;
var
  FolderData: TWin32FileAttributeData;
  Systemtime,LocalTime: _SYSTEMTIME;
  TZ : _TIME_ZONE_INFORMATION;
  DW: DWORD;
begin
  Result := 0;
  FillChar(FolderData, SizeOf(FolderData), 0);
  if GetFileAttributesEx(PChar(AFolder), GetFileExInfoStandard, @FolderData) then
  begin
    FileTimeToSystemTime(FolderData.ftCreationTime, Systemtime);
    GetTimeZoneInformation (TZ);
    SystemTimeToTzSpecificLocalTime(@TZ,Systemtime,LocalTime);
    Result:= SystemTimeToDateTime(LocalTime);
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:51 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz