标题如何实现子目录级的文件查询
栏目软件世界
作者党博昌
发布2001年52期
在使用电脑时,我们经常会用到文件查询功能。通过Win98中提供的查找功能,我们可以方便找出磁盘上任何子目录下的文件。从编程角度讲,它实现了子目录级的文件查询。其实,这项功能并不难实现,关键是要理解并掌握这种程序设计思路。
下面就让我们来看一看在Delphi 5中是如何实现子目录级文件查询的:
程序思路:
首先,应当获取当前目录下的所有子目录名,然后将其存入字符串列表中(TStrings)。其中,用到了几个Delphi的文件系统函数:FindFirst是找出指定目录下第一个文件或目录;FindNext一般和FindFirst配合使用,用来找出下一个文件或目录;FindClose用来关闭查询。接着用FileExists函数查找当前目录,寻找是否有满足条件的文件存在。然后依次从字符串列表中读取下一个子目录名,并使各个子目录成为当前目录(读者到这儿,聪明的你一定想到了“递归”这一编程算法了吧?没错!后面的程序中正是使用了递归的方法来对实现逐个目录的遍历的),最后返回查询结果(以上函数的具体使用方法,请参考本程序)。
经过程序界面设计,下面就是源代码清单:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,Filectrl,
Buttons;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
FLstBox: TFileListBox;
DctBox: TDirectoryListBox;
SpeBtn1: TSpeedButton;
DrvBox: TDriveComboBox;
Label1: TLabel;
STxt1: TStaticText;
btnFind: TSpeedButton;
procedure btnFindClick(Sender: TObject);
procedure FLstBoxChange(Sender: TObject);
procedure DrvBoxChange(Sender: TObject);
procedure DctBoxChange(Sender: TObject);
procedure SpeBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Function SearchFile(MainPath:string;FileName:string;Var FindResult:TStrings):Boolean;
Function IsValidDir (FindResult: TSearchRec): Boolean;
implementation
{$R *.DFM}
//从搜索记录中判断要搜索的内容是否为子目录。
Function IsValidDir(FindResult: TSearchRec): Boolean;
Begin
If (FindResult.Attr=16) and (FindResult.Name<>'.') and (FindResult.Name<>'..') Then
Result :=True
Else
Result := False;
End;
{** 这是查询主体函数 **
参数介绍:MainPath:指定的查询目录。FileName:欲查询的文件。FindResult:返回与欲查找文件相匹配的文件列表,每个文件名均含完整路径。如果有匹配文件,函数返回True,否则,返回False}
Function SearchFile(MainPath:string;FileName:string;Var FindResult:TStrings):Boolean;
Var
i:integer;
IsFound:Boolean;
DirSub: TStrings;
SRecords: TSearchRec;
Begin
IsFound:=False;
If Trim(FileName)<>'' Then
Begin
DirSub:=TStringList.Create;//字符串列表必须动态生成
//找出所有子目录
If FindFirst(MainPath+'*.*',faDirectory)SRecords=0 Then
Begin
If IsValidDir(SRecords) Then DirSub.Add(SRecords.Name);
While (FindNext(SRecords) = 0) Do
Begin
If IsValidDir(SRecords) Then DirSub.Add(SRecords.Name);
End;
End;
FindClose (SRecords);
//查找当前目录
If FileExists (MainPath+ FileName) Then
Begin
IsFound:=True;
FindResult.Add(MainPath + FileName);
End;
//这是递归部分,查找各子目录
For i:=0 to DirSub.Count-1 Do
IsFound:=Searchfile(MainPath+DirSub.Strings[i]+'\',FileName,FindResult) or IsFound;
//资源释放并返回结果
DirSub.Free;
End;
Result:=IsFound;
End;
procedure TForm1.btnFindClick(Sender: TObject);
Var
FindRec:TStrings;
i:integer;
begin
Memo1.Clear ;
FindRec:=TStringList.Create;
if SearchFile(Edit1.text,Edit2.text,FindRec)=True then
if FindRec.Count =0 then ShowMessage('未找到文件')
else
For i:=0 to FindRec.Count-1 do
memo1.Lines.Add(FindRec.Strings[i]);
STxt1.Caption:=IntToStr(FindRec.Count);
FindRec.free;
end;
procedure TForm1.FLstBoxChange(Sender: TObject);
begin
edit2.Text:=ExtractFileName(FLstBox.FileName );
end;
procedure TForm1.DrvBoxChange(Sender: TObject);
begin
Try
DctBox.Drive:=DrvBox.Drive ;
Except
showMessage('驱动器未准备好');
end;
end;
procedure TForm1.DctBoxChange(Sender: TObject);
begin
Try
FLstBox.Directory:=DctBox.Directory ;
Edit1.Text :=DctBox.Directory;
if copy(edit1.text,strlen(pchar(Edit1.text)),1)<> '\' then
Edit1.Text :=Edit1.text +'\';
DrvBox.Drive :=DctBox.Drive ;
Except
showMessage('文件目录不存在');
end;
end;
procedure TForm1.SpeBtn1Click(Sender: TObject);
Var
SDir: string;
begin
Try
SelectDirectory('请选择一个要查找文件的目录:','',SDir;
Edit1.Text :=SDir;
DctBox.Directory:=Sdir;
Except
ShowMessage('选择目录错误!');
End;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text:=DctBox.Directory;
if copy(edit1.text,strlen (pchar(Edit1.text)),1)<> '\' then
Edit1.Text :=Edit1.text +'\';
DrvBox.Drive :=DctBox.Drive ;
end;
end.
注:本程序在“Windows 98SE中文版+Delphi 5”下测试通过。怎么样?看完了以上的程序代码,是不是感觉并不怎么难啊?其实无论要设计一个什么软件,最重要的是先要考虑出一个可行的编程思路。