我测试过了,没问题了,自己再优化下吧
借助了FileListBox控件
可以的话,结帖给分哦!!! 呵呵
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
ListBox1: TListBox;
Edit2: TEdit;
OpenDialog1: TOpenDialog;
FileListBox1: TFileListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function IsValidDir(SearchRec:TSearchRec):Boolean;
function SearchFile(mainpath:string;filename:string;var foundresult:TStrings):Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.IsValidDir(SearchRec: TSearchRec): Boolean;
begin
if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
result:=true
else
result:=false;
end;
function TForm1.SearchFile(mainpath, filename: string;
var foundresult: TStrings): Boolean;
var
i:Integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TSearchRec;
s:string;
begin
Found:=false;
if trim(filename)<>'' then
begin
subdir1:=TStringList.Create;
if (FindFirst(mainpath+'*.*',faDirectory,searchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec)=0 ) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
FileListBox1.Visible:=false;
FileListBox1.Mask:='*'+filename;
FileListBox1.Directory:=mainpath;
for i:=0 to FileListBox1.Items.Count-1 do
begin
if FileExists(FileListBox1.Items.Strings) then
s:= FileListBox1.Items.Strings;
begin
Found:=true;
foundresult.Add(mainpath+s);
end;
end;
for i:=0 to subdir1.Count-1 do
found:=SearchFile(mainpath+subdir1.Strings+'/',FileName,foundresult);
subdir1.Free;
end;
result:=found;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s:TStrings;
begin
s:=TStringList.Create;
SearchFile(edit1.Text,edit2.Text,s);
ListBox1.Items.AddStrings(s);
s.Free;
end;
end.