A、如果不包括子目录则直接将有关子目录的循环直接注释即可 B、在调用本函数之前必须将FileList初始化 如FileList:=TStringList.Create; C、 sRoot表示需要遍历的目录全名 D、函数在D7/D6下测试通过 function DoDirTree(sRoot:String;var FileList:TStrings):boolean; var i:integer; Sr : TSearchRec; Err : integer; //TrSize, FilePath : string; lsPath:TStrings; Begin if Copy(sRoot,Length(sRoot),1)<>'\' then sRoot := sRoot +'\'; Err:=FindFirst(sRoot+'*.*',$37,Sr); While (Err = 0) do begin If (Sr.Attr and faDirectory)=0 then FileList.Add('2'+sRoot +Sr.Name) else if (Sr.Name[1] <> '.') then DoDirTree(sRoot,FileList) ; Err:=FindNext(Sr) ; Application.ProcessMessages; end ; Err:=FindFirst(sRoot + '*.',$37,Sr); lsPath := TStringList.Create; While (Err = 0) do begin if ((Sr.Attr and faDirectory)<>0)and (Sr.Name[1] <> '.') then if DirectoryExists(sRoot + sr.Name) then lsPath.Add(sRoot + sr.Name); Err:=FindNext(Sr) ; Application.ProcessMessages; end ; for i:=0 to lsPath.Count -1 do DoDirTree(lsPath.Strings[i],FileList); lsPath.Free; Result := True; End;
procedure FindAll(Disk,Path: String; var FileName: TStringList); var Fpath: String; FS: TsearchRec; begin Fpath:=Disk+Path+'\*.*'; if FindFirst( Fpath,faAnyFile,FS ) = 0 then begin if ( FS.Name <> '.' ) and ( FS.Name <> '..' ) then if ( FS.Attr and faDirectory ) = faDirectory then FindAll( Disk,Path+'\'+FS.Name,FileName ) else FileName.Add( FS.Name ); while findnext( FS )=0 do begin if ( FS.Name <> '.' ) and ( FS.Name <> '..' ) then if ( FS.Attr and faDirectory ) = faDirectory then FindAll( Disk,Path+'\'+FS.Name,FileName ) else FileName.add( FS.Name); end; end; Findclose( FS ); end; procedure TForm1.Button2Click(Sender: TObject); var FileResult: TStringList; begin FileResult := TStringList.Create; FileResult.Clear; FindAll(Edit1.Text,Edit2.Text, FileResult); showmessage( Fileresult.Text ); FreeAndNil ( FileResult ); end;
B、在调用本函数之前必须将FileList初始化
如FileList:=TStringList.Create;
C、 sRoot表示需要遍历的目录全名
D、函数在D7/D6下测试通过
function DoDirTree(sRoot:String;var FileList:TStrings):boolean;
var
i:integer;
Sr : TSearchRec;
Err : integer;
//TrSize, FilePath : string;
lsPath:TStrings;
Begin if Copy(sRoot,Length(sRoot),1)<>'\' then
sRoot := sRoot +'\';
Err:=FindFirst(sRoot+'*.*',$37,Sr);
While (Err = 0) do
begin
If (Sr.Attr and faDirectory)=0 then
FileList.Add('2'+sRoot +Sr.Name)
else
if (Sr.Name[1] <> '.') then
DoDirTree(sRoot,FileList) ; Err:=FindNext(Sr) ;
Application.ProcessMessages;
end ; Err:=FindFirst(sRoot + '*.',$37,Sr);
lsPath := TStringList.Create;
While (Err = 0) do
begin
if ((Sr.Attr and faDirectory)<>0)and (Sr.Name[1] <> '.') then
if DirectoryExists(sRoot + sr.Name) then
lsPath.Add(sRoot + sr.Name);
Err:=FindNext(Sr) ;
Application.ProcessMessages;
end ;
for i:=0 to lsPath.Count -1 do
DoDirTree(lsPath.Strings[i],FileList);
lsPath.Free;
Result := True;
End;
var
Fpath: String;
FS: TsearchRec;
begin
Fpath:=Disk+Path+'\*.*';
if FindFirst( Fpath,faAnyFile,FS ) = 0 then
begin
if ( FS.Name <> '.' ) and ( FS.Name <> '..' ) then
if ( FS.Attr and faDirectory ) = faDirectory then FindAll( Disk,Path+'\'+FS.Name,FileName )
else
FileName.Add( FS.Name );
while findnext( FS )=0 do
begin
if ( FS.Name <> '.' ) and ( FS.Name <> '..' ) then
if ( FS.Attr and faDirectory ) = faDirectory then FindAll( Disk,Path+'\'+FS.Name,FileName )
else
FileName.add( FS.Name);
end;
end;
Findclose( FS );
end;
procedure TForm1.Button2Click(Sender: TObject);
var
FileResult: TStringList;
begin
FileResult := TStringList.Create;
FileResult.Clear;
FindAll(Edit1.Text,Edit2.Text, FileResult);
showmessage( Fileresult.Text );
FreeAndNil ( FileResult );
end;