通过递归来实现搜索文件
点击次数:44 次 发布日期:2008-11-09 08:42:17 作者:源代码网
|
源代码网推荐 1、窗体设计 源代码网推荐 新建一个工程,在Form1中添加DriveComboBox1、Edit1、Listbox1、Button1、DirectoryOutline1、Label1,把Edit1的Text属性改为*.*,Button1的Caption属性改为"查找",各个控件布局如下图: 源代码网推荐 源代码网推荐 源代码网推荐 2、程序清单 源代码网推荐 unit main; 源代码网推荐 源代码网推荐 interface 源代码网推荐 源代码网推荐 uses 源代码网推荐 Windows, Messages, SysUtils, Classes, Graphics, 源代码网推荐 Controls, Forms, Dialogs,stdctrls,filectrl,grids,outline,diroutln; 源代码网推荐 源代码网推荐 type 源代码网推荐 TForm1 = class(TForm) 源代码网推荐 DriveComboBox1: TDriveComboBox; 源代码网推荐 Edit1: TEdit; 源代码网推荐 Listbox1: TListBox; 源代码网推荐 Button1: TButton; 源代码网推荐 Label1: TLabel; 源代码网推荐 DirectoryOutline1: TDirectoryOutline; 源代码网推荐 procedure Button1Click(Sender: TObject); 源代码网推荐 procedure DriveComboBox1Change(Sender: TObject); 源代码网推荐 private 源代码网推荐 { Private declarations } 源代码网推荐 ffilename:string; 源代码网推荐 function getdirectoryname(dir:string):string; 源代码网推荐 procedure findfiles(apath:string); 源代码网推荐 public 源代码网推荐 { Public declarations } 源代码网推荐 end; 源代码网推荐 源代码网推荐 var 源代码网推荐 Form1: TForm1; 源代码网推荐 t:integer; 源代码网推荐 implementation 源代码网推荐 源代码网推荐 {$R *.DFM} 源代码网推荐 function tForm1.getdirectoryname(dir:string):string; 源代码网推荐 {对文件名进行转换,使之以反斜杠结尾} 源代码网推荐 begin 源代码网推荐 if dir[length(dir)]<>"" then 源代码网推荐 result:=dir "" 源代码网推荐 else 源代码网推荐 result:=dir; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 procedure TForm1.findfiles(apath: string); 源代码网推荐 {通过递归调用,可以在当前目录和子目录下查找指定格式的文件} 源代码网推荐 var 源代码网推荐 fsearchrec,dsearchrec:tsearchrec; 源代码网推荐 findresult:integer; 源代码网推荐 function isdirnotation(adirname:string):boolean; 源代码网推荐 begin 源代码网推荐 result:=(adirname=".") or (adirname=".."); 源代码网推荐 end; 源代码网推荐 begin 源代码网推荐 源代码网推荐 apath:=getdirectoryname(apath); //获取一个有效的目录名称 源代码网推荐 源代码网推荐 {查找一个匹配的文件} 源代码网推荐 findresult:=findfirst(apath ffilename,faanyfile fahidden fasysfile fareadonly,fsearchrec); 源代码网推荐 try 源代码网推荐 {继续查找匹配的文件} 源代码网推荐 while findresult=0 do 源代码网推荐 begin 源代码网推荐 Listbox1.Items.Add(lowercase(apath fsearchrec.Name)); 源代码网推荐 t:=t 1; 源代码网推荐 label1.Caption:=inttostr(t); 源代码网推荐 findresult:=findnext(fsearchrec); 源代码网推荐 end; 源代码网推荐 源代码网推荐 {在当前目录的子目录中进行查找} 源代码网推荐 findresult:=findfirst(apath "*.*",fadirectory,dsearchrec); 源代码网推荐 while findresult=0 do 源代码网推荐 begin 源代码网推荐 if ((dsearchrec.Attr and fadirectory)=fadirectory) and not 源代码网推荐 isdirnotation(dsearchrec.Name) then 源代码网推荐 findfiles(apath dsearchrec.Name);//在此处是递归调用 源代码网推荐 findresult:=findnext(dsearchrec); 源代码网推荐 end; 源代码网推荐 源代码网推荐 finally 源代码网推荐 findclose(fsearchrec); 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 procedure TForm1.Button1Click(Sender: TObject); 源代码网推荐 {调用FindFiles()函数,用来搜索子目录} 源代码网推荐 begin 源代码网推荐 t:=0; 源代码网推荐 screen.Cursor:=crhourglass; 源代码网推荐 try 源代码网推荐 Listbox1.Items.Clear; 源代码网推荐 ffilename:=Edit1.Text; 源代码网推荐 findfiles(DirectoryOutline1.Directory); 源代码网推荐 finally 源代码网推荐 screen.Cursor:=crdefault; 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 procedure TForm1.DriveComboBox1Change(Sender: TObject); 源代码网推荐 begin 源代码网推荐 DirectoryOutline1.Drive:=DriveComboBox1.Drive; 源代码网推荐 end; 源代码网推荐 源代码网推荐 end. 源代码网推荐 源代码网推荐 本程序在Win2000/Delphi6中运行通过。 源代码网推荐 (广州 叶海河) 源代码网推荐 源代码网供稿. |
