List file in Directory and sub directory with Delphi 7


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure AddAllFilesInDir(const Dir: string; List: TStrings);
    function FileLastModifiedAge(const TheFile: string): TDateTime;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DateUtils;

function TForm1.FileLastModifiedAge(const TheFile: string): TDateTime;
begin
     if FileExists(TheFile) then
        Result:= FileDateToDateTime(FileAge(TheFile))
     else
        Result:= encodedate(1899,12,31);
end;

procedure TForm1.AddAllFilesInDir(const Dir: string; List: TStrings);
var
  SR: TSearchRec;
begin
  if FindFirst(IncludeTrailingPathDelimiter(Dir) + '*.*', faAnyFile or faDirectory, SR) = 0 then
    try
      repeat
        if (SR.Attr and faDirectory) = 0 then
          List.Add(format('%s\%s File Age: %s',[Dir,SR.Name,
          FormatDateTime('dd/mm/yyyy HH:NN:SS',FileLastModifiedAge(format('%s\%s',[Dir,SR.Name])))]))
        else if (SR.Name <> '.') and (SR.Name <> '..') then
          AddAllFilesInDir(IncludeTrailingPathDelimiter(Dir) + SR.Name, List);  // recursive call!
      until FindNext(Sr) <> 0;
    finally
      FindClose(SR);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Memo1.Lines.BeginUpdate;
    Memo1.Clear;
    AddAllFilesInDir(trim(edit1.text),Memo1.lines);
  finally
    Memo1.Lines.EndUpdate;
  end;
end;

end.


Comments