The following example uses an edit control, a button, a string grid, and seven check boxes. The check boxes correspond to the seven possible file attributes. When the button is clicked, the path specified in the edit control is searched for files matching the checked file attributes. The names and sizes of the matching files are inserted into the string grid.procedure TForm1.Button1Click(Sender: TObject);var sr: TSearchRec; FileAttrs: Integer; begin StringGrid1.RowCount := 1; if CheckBox1.Checked then FileAttrs := faReadOnly else FileAttrs := 0; if CheckBox2.Checked then FileAttrs := FileAttrs + faHidden; if CheckBox3.Checked then FileAttrs := FileAttrs + faSysFile; if CheckBox4.Checked then FileAttrs := FileAttrs + faVolumeID; if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory; if CheckBox6.Checked then FileAttrs := FileAttrs + faArchive; if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do begin RowCount := 0; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then begin repeat if (sr.Attr and FileAttrs) = sr.Attr then begin RowCount := RowCount + 1; Cells[1,RowCount-1] := sr.Name; Cells[2,RowCount-1] := IntToStr(sr.Size); end; until FindNext(sr) <> 0; FindClose(sr); end; end; end;
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do
begin
RowCount := 0; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;
unit Main;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
SearchBtn: TButton;
DirectoryEdt: TMemo;
PathEdt: TEdit;
Label1: TLabel;
Image1: TImage;
procedure SearchBtnClick(Sender: TObject);
procedure MakeTree;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.MakeTree;
var
Sr: TSearchRec;
Err: Integer;
FilePath: string;
begin
Err := FindFirst('*.*',$37,Sr); //$37为除Volumn ID Files外的所有文件
// 如果找到文件
while (Err = 0) do
begin
if Sr.Name[1] <> '.' then
begin
//找到文件
if (Sr.Attr and faDirectory) = 0 then
begin end;
//找到子目录
if (Sr.Attr and faDirectory) = 16 then
begin
FilePath := ExpandFileName(Sr.Name);
DirectoryEdt.Lines.Add(FilePath);
ChDir(Sr.Name);
MakeTree;
ChDir('..');
end;
end; //结束递归
Err := FindNext(Sr);
end;
end;procedure TForm1.SearchBtnClick(Sender: TObject);
begin
DirectoryEdt.Lines.Clear;
ChDir(PathEdt.Text);
MakeTree;
end;end.
这是个递归搜文件的例子,