如果我想查找一个目录下的所有文件,包括子目录中的文件,要怎么做
我用FindFirst and FindNext,只能查找到当前目录下的文件,子目录中的东西无法查找,有怎么作,有没有什么函数。

解决方案 »

  1.   

    unit FileSerach;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls, ExtCtrls;type
      Tfilefindfrm = class(TForm)
        lbfiles: TListBox;
        Panel1: TPanel;
        Button1: TButton;
        Edit2: TEdit;//目录
        Edit1: TEdit;//文件名如*.Txt等
        Label1: TLabel;
        Label2: TLabel;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
      public
      FFileName:String;  //查找的文件名
      function GetDirectoryName(Dir:String):String;
      Procedure FindFiles(APath:String);
        { Public declarations }
      end;var
      filefindfrm: Tfilefindfrm;implementation{$R *.dfm}
    function Tfilefindfrm.GetDirectoryName(Dir:String):String;  //设置DIR
    begin
     if Dir[LengTh(Dir)]<>'\' then
      Result:=Dir+'\'
     else
     Result:=Dir;
    end;procedure TfilefindFrm.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
     lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));    //把查找到的文件列出来
     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 Tfilefindfrm.Button1Click(Sender: TObject);
    begin
      Screen.Cursor:=CrHourGlass;
      try
      lbFiles.Items.Clear;
      FFIleName:=Edit2.Text;
      FindFiles(Edit1.Text);
      Finally
      Screen.Cursor:=crDefault;
    end;
    end;end.