?牐犗旅婢腿梦颐抢纯匆豢丛贒elphi 5中是如何实现子目录级文件查询的:
?牐牫绦蛩悸罚?
?牐犑紫龋Φ被袢〉鼻澳柯枷碌乃凶幽柯济缓蠼浯嫒胱址斜碇校═Strings)。其中,用到了几个Delphi的文件系统函数:FindFirst是找出指定目录下第一个文件或目录;FindNext一般和FindFirst配合使用,用来找出下一个文件或目录;FindClose用来关闭查询。接着用FileExists函数查找当前目录,寻找是否有满足条件的文件存在。然后依次从字符串列表中读取下一个子目录名,并使各个子目录成为当前目录(读者到这儿,聪明的你一定想到了“递归”这一编程算法了吧?没错!后面的程序中正是使用了递归的方法来对实现逐个目录的遍历的),最后返回查询结果(以上函数的具体使用方法,请参考本程序)。
?牐牼绦蚪缑嫔杓疲旅婢褪窃创肭宓ィ?
?牐爑nit Unit1;
?牐爄nterface
?牐爑ses
?牐燱indows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,Filectrl,
?牐燘uttons;
?牐爐ype
?牐燭Form1 = class(TForm)
?牐燛dit1: TEdit;
?牐燛dit2: TEdit;
?牐燤emo1: TMemo;
?牐燜LstBox: TFileListBox;
?牐燚ctBox: TDirectoryListBox;
?牐燬peBtn1: TSpeedButton;
?牐燚rvBox: TDriveComboBox;
?牐燣abel1: TLabel;
?牐燬Txt1: TStaticText;
?牐燽tnFind: TSpeedButton;
?牐爌rocedure btnFindClick(Sender: TObject);
?牐爌rocedure FLstBoxChange(Sender: TObject);
?牐爌rocedure DrvBoxChange(Sender: TObject);
?牐爌rocedure DctBoxChange(Sender: TObject);
?牐爌rocedure SpeBtn1Click(Sender: TObject);
?牐爌rocedure FormCreate(Sender: TObject);
?牐爌rivate
?牐爗 Private declarations }
?牐爌ublic
?牐爗 Public declarations }
?牐爀nd;
?牐爒ar
?牐燜orm1: TForm1;
?牐燜unction SearchFile(MainPath:string;FileName:string;Var FindResult:TStrings):Boolean;
?牐燜unction IsValidDir (FindResult: TSearchRec): Boolean;
?牐爄mplementation
?牐爗$R *.DFM}
?牐?//从搜索记录中判断要搜索的内容是否为子目录。
?牐燜unction IsValidDir?煟‵indResult: TSearchRec): Boolean;
?牐燘egin
?牐營f (FindResult.Attr=16) and (FindResult.Name<>'.') and (FindResult.Name<>'..') Then
?牐燫esult :=True
?牐燛lse
?牐燫esult := False;
?牐燛nd;
?牐爗** 这是查询主体函数 **
?牐牪问樯埽篗ainPath:指定的查询目录。FileName:欲查询的文件。FindResult:返回与欲查找文件相匹配的文件列表,每个文件名均含完整路径。如果有匹配文件,函数返回True,否则,返回False}
?牐燜unction SearchFile?煟∕ainPath:string;FileName:string;Var FindResult:TStrings):?烞oolean;
?牐燰ar
?牐爄:integer;
?牐營sFound:Boolean;
?牐燚irSub: TStrings;
?牐燬Records: TSearchRec;
?牐燘egin
?牐營sFound:=False;
?牐營f Trim(FileName)<>'' Then
?牐燘egin
?牐燚irSub:=TStringList.Create;//字符串列表必须动态生成
?牐?//找出所有子目录
?牐營f FindFirst(MainPath+'*.*',faDirectory)SRecords??=0 Then
?牐燘egin
?牐營f IsValidDir(SRecords) Then DirSub.Add(SRecords.Name);
?牐燱hile (FindNext(SRecords) = 0) Do
?牐燘egin
?牐營f IsValidDir(SRecords) Then DirSub.Add(SRecords.Name);
?牐燛nd;
?牐燛nd;
?牐燜indClose (SRecords);
?牐?//查找当前目录
?牐營f FileExists (MainPath+ FileName) Then
?牐燘egin
?牐營sFound:=True??
?牐燜indResult.Add?煟∕ainPath + FileName);
?牐燛nd;
?牐?//这是递归部分,查找各子目录
?牐燜or i:=0 to DirSub.Count-1 Do
?牐營sFound:=Searchfile(MainPath+DirSub.Strings[i]+'\',FileName,FindResult) or IsFound;
?牐?//资源释放并返回结果
?牐燚irSub.Free;
?牐燛nd;
?牐燫esult:=IsFound;
?牐燛nd;
?牐爌rocedure TForm1.btnFindClick(Sender: TObject);??
?牐燰ar
?牐燜indRec:TStrings;
?牐爄:integer;
?牐燽egin
?牐燤emo1.Clear ;
?牐燜indRec:=TStringList.Create;
?牐爄f SearchFile?煟‥dit1.text,Edit2.text,FindRec)=True then
?牐爄f FindRec.Count =0 then ShowMessage('未找到文件')
?牐爀lse
?牐燜or i:=0 to FindRec.Count-1 do
?牐爉emo1.Lines.Add(FindRec.Strings[i]);
?牐燬Txt1.Caption :=IntToStr?煟‵indRec.Count);
?牐燜indRec.free;
?牐爀nd;
?牐爌rocedure TForm1.FLstBoxChange(Sender: TObject);
?牐燽egin
?牐爀dit2.Text :??=ExtractFileName?煟‵LstBox.FileName );
?牐爀nd;
?牐爌rocedure TForm1.DrvBoxChange(Sender: TObject);
?牐燽egin
?牐燭ry
?牐燚ctBox.Drive :=DrvBox.Drive ;
?牐燛xcept
?牐爏howMessage('驱动器未准备好');
?牐爀nd;
?牐爀nd;
?牐爌rocedure TForm1.DctBoxChange(Sender: TObject);
?牐燽egin
?牐燭ry
?牐燜LstBox.Directory:=DctBox.Directory ;
?牐燛dit1.Text :=DctBox.Directory;
?牐爄f copy(edit1.text,strlen?煟╬char(Edit1.text)),1)<> '\' then
?牐燛dit1.Text :=Edit1.text +'\';
?牐燚rvBox.Drive :=DctBox.Drive ;
?牐燛xcept
?牐爏howMessage('文件目录不存在');
?牐爀nd;
?牐爀nd;
?牐爌rocedure TForm1.SpeBtn1Click(Sender: TObject);
?牐燰ar
?牐燬Dir : string;
?牐燽egin
?牐燭ry
?牐燬electDirectory('请选择一个要查找文件的目录:','',SDir;
?牐燛dit1.Text :=SDir;
?牐燚ctBox.Directory:??=Sdir;
?牐燛xcept
?牐燬howMessage('选择目录错误!');
?牐燛nd;
?牐爀nd;
?牐爌rocedure TForm1.FormCreate(Sender: TObject);
?牐燽egin
?牐燛dit1.Text:=DctBox.Directory;
?牐爄f copy(edit1.text,strlen (pchar(Edit1.text)),1??<> '\' then
?牐燛dit1.Text :=Edit1.text +'\';
?牐燚rvBox.Drive :=DctBox.Drive ;
?牐爀nd;
?牐爀nd.
?牐犠ⅲ罕境绦蛟凇癢indows 98SE中文版+Delphi 5”下测试通过。怎么样?看完了以上的程序代码,是不是感觉并不怎么难啊?其实无论要设计一个什么软件,最重要的是先要考虑出一个可行的编程思路。