function IsValidDir(SearchRec:TSearchRec):Boolean; begin if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then Result:=True else Result:=False; end;function SearchFile(mainpath:string; filename:string; var foundresult:TStrings):Boolean; var i:integer; Found:Boolean; subdir1:TStrings; searchfl,searchRec:TsearchRec; 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 FIndfirst(mainpath+filename,faAnyFile,searchfl)=0 then begin found:=true; foundresult.Add(mainpath+searchfl.Name); while (FindNext(searchfl)=0) do begin foundresult.Add(mainpath+searchfl.Name); end; end; FindCLose(searchfl); for i:=0 to subdir1.Count-1 do found:=Searchfile(mainpath+subdir1.Strings[i]+'\',Filename,foundresult)or found; subdir1.Free; end; result:=found; End; filename:=*.*;
多多?findfirst() findNext
多多?FindFirstFile() FindNextFile()
procedure TForm1.Button1Click(Sender: TObject); var ss:Tsearchrec; filepath:string; begin filepath:='c:\'; listbox1.Items.Clear; if findfirst(filepath+'*.*',faAnyFile,ss)=0 then begin if not ((ss.Attr and fadirectory)=fadirectory) then listbox1.Items.Add(ss.Name); while findnext(ss)=0 do begin if not ((ss.attr and fadirectory)=fadirectory) then listbox1.Items.Add(ss.Name); end; findclose(ss); end; end;
如 var list:=Tstringlist; begin list:=Tstringlist.create;SearchFile(c:\,*.exe,list);for i:=list.count-1 do showmessage(list.string[i]);
枚举文件夹下面的信息 //查找指定目录下所有TXT文件名,赋给MEMO procedure TForm1.Button1Click(Sender: TObject); var i,tag:integer; rec:TSearchRec; ExtNames:TStrings; begin ExtNames:=TStringList.Create; i:=FindFirst('C:\*.*',faAnyFile,rec); tag:=0; while i=0 do begin ExtNames.Add(rec.name); memo1.Lines.Add(ExtNames[tag]); i:=FindNext(Rec); inc(tag); end; FindClose(rec); end;
procedure TForm1.Button1Click(Sender: TObject); var f:tsearchrec; s:string; begin if edit1.Text='' then exit; if edit1.text[length(edit1.text)]<>'\' then s:=edit1.Text+'\*.*' else s:=edit1.Text+'*.*'; findfirst(s,faanyfile,f); memo1.Lines.Add(f.name); while findnext(f)=0 do memo1.Lines.Add(f.name);end;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;function SearchFile(mainpath:string; filename:string;
var foundresult:TStrings):Boolean;
var
i:integer;
Found:Boolean;
subdir1:TStrings;
searchfl,searchRec:TsearchRec;
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 FIndfirst(mainpath+filename,faAnyFile,searchfl)=0 then begin
found:=true;
foundresult.Add(mainpath+searchfl.Name);
while (FindNext(searchfl)=0) do begin
foundresult.Add(mainpath+searchfl.Name);
end;
end;
FindCLose(searchfl);
for i:=0 to subdir1.Count-1 do
found:=Searchfile(mainpath+subdir1.Strings[i]+'\',Filename,foundresult)or found;
subdir1.Free;
end;
result:=found;
End;
filename:=*.*;
findNext
FindNextFile()
var ss:Tsearchrec;
filepath:string;
begin
filepath:='c:\';
listbox1.Items.Clear;
if findfirst(filepath+'*.*',faAnyFile,ss)=0 then
begin
if not ((ss.Attr and fadirectory)=fadirectory) then
listbox1.Items.Add(ss.Name);
while findnext(ss)=0 do
begin
if not ((ss.attr and fadirectory)=fadirectory) then
listbox1.Items.Add(ss.Name);
end;
findclose(ss);
end;
end;
var
list:=Tstringlist;
begin
list:=Tstringlist.create;SearchFile(c:\,*.exe,list);for i:=list.count-1 do
showmessage(list.string[i]);
//查找指定目录下所有TXT文件名,赋给MEMO
procedure TForm1.Button1Click(Sender: TObject);
var
i,tag:integer;
rec:TSearchRec;
ExtNames:TStrings;
begin
ExtNames:=TStringList.Create;
i:=FindFirst('C:\*.*',faAnyFile,rec);
tag:=0;
while i=0 do
begin
ExtNames.Add(rec.name);
memo1.Lines.Add(ExtNames[tag]);
i:=FindNext(Rec);
inc(tag);
end;
FindClose(rec);
end;
var f:tsearchrec;
s:string;
begin if edit1.Text='' then exit;
if edit1.text[length(edit1.text)]<>'\' then
s:=edit1.Text+'\*.*'
else s:=edit1.Text+'*.*';
findfirst(s,faanyfile,f); memo1.Lines.Add(f.name); while findnext(f)=0 do
memo1.Lines.Add(f.name);end;