TDirectory.GetFiles 函數還有一個 TDirectory.TFilterPredicate 類型的參數, 這是個匿名函數類型, 可對搜索結果再處理.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IOUtils, Types;
const path = 'C:\Program Files\Embarcadero\RAD Studio\7.0';
//准備給 TDirectory.GetFiles 調用的函數, 須符合 TDirectory.TFilterPredicate 格式
function fp(const Path: string; const SearchRec: TSearchRec): Boolean;
begin
Form1.Memo1.Lines.Add(Path + '\' + SearchRec.Name);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
files: TStringDynArray;
begin
Memo1.Clear;
files := TDirectory.GetFiles(path, fp);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
files: TStringDynArray;
begin
Memo1.Clear;
files := TDirectory.GetFiles(path, '*.txt', fp);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
files: TStringDynArray;
begin
Memo1.Clear;
files := TDirectory.GetFiles(path, '*.txt', TSearchOption.soAllDirectorIEs, fp);
end;
//可以這樣方便地使用匿名函數
procedure TForm1.Button4Click(Sender: TObject);
var
files: TStringDynArray;
begin
Memo1.Clear;
files := TDirectory.GetFiles(path,
function(const Path: string; const SearchRec: TSearchRec): Boolean
begin
Memo1.Lines.Add(Path + '\' + SearchRec.Name);
end
);
end;
end.