TDirectory.GetFiles with DateTime filter

ISIGest
ISIGest used Ask the Experts™
on
Hi, how can I use the new TDirectory.GetFiles function with Delphi 2010 by filtering the result with a DateTime interval on last edit of files.

Sorry for my english.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
This will list files from the current directory which were modified within last 24 hours. Don't forget to drop TMemo on your form.
uses
  Types, IOUtils, DateUtils;

function FileTimeToDateTime(const FileTime: TFileTime): TDateTime;
var
  LocalFileTime: TFileTime;
  SystemTime: TSystemTime;
begin
  FileTimeToLocalFileTime(FileTime, LocalFileTime);
  FileTimeToSystemTime(LocalFileTime, SystemTime);
  Result := SystemTimeToDateTime(SystemTime);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Filter: TDirectory.TFilterPredicate;
  DateStart, DateStop: TDateTime;
  Files: TStringDynArray;
  Path: String;
begin
  DateStop := Now;
  DateStart := IncDay(DateStop, -1);
  Filter := function(const Path: string; const SearchRec: TSearchRec): Boolean
    var
      DT: TDateTime;
    begin
      DT := FileTimeToDateTime(SearchRec.FindData.ftLastWriteTime);
      Result := (DateStart <= DT) and (DT <= DateStop);
    end;
  Files := TDirectory.GetFiles(TDirectory.GetCurrentDirectory, Filter);
  for Path in Files do
    Memo1.Lines.Add(Path);
end;

Open in new window

Ferruccio AccalaiSenior developer, analyst and customer assistance
Commented:
I was coding almost the same and found that den4balready provided a working solution

Well, btw I add my example with recursive search in subdirectories also.

Add a tbutton and a TlistBox to the form then past the code.




unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IOUtils, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure SearchBetweenDates(StartDir: String; DateFrom, DateTo: TDateTime;
      const List: TStrings);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
SearchBetweenDates('c:\', STrToDate('17/01/2010'), STrToDate('19/01/2010'),Listbox1.Items);
end;

procedure TForm1.SearchBetweenDates(StartDir: string; DateFrom, DateTo: TDateTime; const List: TStrings);
var
  Directory, Path: string;
  FilterPredicate: TDirectory.TFilterPredicate;
  SO: TSearchOption;

begin
  Screen.Cursor := crHourGlass;

  SO := TSearchOption.soAllDirectories;
  FilterPredicate := function(const Path: string; const SearchRec: TSearchRec): Boolean
  begin
    Result := (FIleDatetoDateTime(SearchRec.FindData.ftLastWriteTime) >= DateFrom) AND
      (FIleDatetoDateTime(SearchRec.FindData.ftLastWriteTime) <= DateTo) AND
      (SearchRec.Attr = faArchive);
  end;
  for Directory in TDirectory.GetDirectories(StartDir, SO, nil) do
    for Path in TDirectory.GetFiles(Directory, FilterPredicate) do
    begin
      List.Add(Path);
      Application.ProcessMessages;
    end;
  screen.Cursor := crDefault;
end;

end.

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial