Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Get last created folder name from a given path

Tags:

delphi

Is there a function to get the last created folder from a given path? I want to see the last created folder in order to check if my camera has taken photos today. Another approach I was thinking was to get the system date and then start searching for a folder that contained the current date.However if the camera date is wrong then this approach wont work! Thanks. Any other ideas?

ex:

if lastcreatedfolder(dir_path):='05012016' then 
showmessage('TODAY A FOLDER WAS CREATED') 
else 
showmessage('NO FOLDER WAS CREATED TODAY!');
like image 229
user2858981 Avatar asked Mar 15 '26 11:03

user2858981


2 Answers

Delphi 2010 also has the IOUtils.pas unit.

Using this unit, the last created folder may be found as follows:

uses
  IOUtils, Types, DateUtils;

function FindLastCreatedDirectory(const APath: string): string;
var
  LastCreateTime : TDateTime;
  PathsInQuestion: TStringDynArray;
  n : Integer;
begin
  LastCreateTime := MinDateTime;
  Result := '';

  PathsInQuestion := TDirectory.GetDirectories(APath);
  for n := Low(PathsInQuestion) to High(PathsInQuestion) do
  begin
    if CompareDateTime(TDirectory.GetCreationTime(PathsInQuestion[n]), LastCreateTime) = GreaterThanValue then
    begin
      LastCreateTime := TDirectory.GetCreationTime(PathsInQuestion[n]);
      Result := PathsInQuestion[n];
    end;
  end;
end;
like image 160
Christian Holm Jørgensen Avatar answered Mar 17 '26 02:03

Christian Holm Jørgensen


The last created directory in a given path can be found using the System.SysUtils.FindFirst function.

The TimeStamp field of the TSearchRec record can be checked using the function's var F parameter in order to evaluate the timestamp of a file system element.

uses
  System.SysUtils,
  Winapi.Windows;

function getLastCreatedDirectory(const APath: string): string;
var
  res: TSearchRec;
  lastCreatedFileTime: TFileTime;
begin
  Result := '';
  FillChar(lastCreatedFileTime, SizeOf(TFileTime), 0);

  if FindFirst(APath, faDirectory, res) = 0 then begin
    try
      repeat
        if (res.Attr and faDirectory) = 0 then
          Continue;

        if (res.Name = '.') or (res.Name = '..') then
          Continue;

        {if res.TimeStamp > lastCreatedTime then begin
          lastCreatedTime := res.TimeStamp;
          Result := ExtractFilePath(APath) + res.Name;
        end;}

        if CompareFileTime(res.FindData.ftCreationTime, lastCreatedFileTime) = 1 then begin
          lastCreatedFileTime := res.FindData.ftCreationTime;
          Result := ExtractFilePath(APath) + res.Name;
        end;

      until FindNext(res) <> 0;
    finally
      System.SysUtils.FindClose(res);
    end;
  end;
end;

begin
  WriteLn(getLastCreatedDirectory('C:\Program Files (x86)\*'));
  ReadLn;
end.

EDIT 2
Since res.TimeStamp seems to give the last modified date and the TSearchRec.Time field has been deprecated, the folder's creation time can be obtained evaluating the res.FindData.ftCreationTime field of the TSearchRec record.

like image 33
fantaghirocco Avatar answered Mar 17 '26 04:03

fantaghirocco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!