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);
if (copy(mainpath,length(mainpath)-length(filename),length(filename))=filename) then
begin
Found:=true;
foundresult.Add(mainpath);
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.