我有个搜索包括子目录的程序,我是从C++程序改过来的,要是有什么不过请指教(本人不是Dephi程序员)
你要是不想去搜索子目录,请用//标记的行适当去掉并修改
FileList TStringList;
procedure FindIntheDirectry(theDirectry : string );
var 
sr: TSearchRec;
FileAttrs: Integer;
begin if FindFirst(theDirectry, FileAttrs, sr) = 0 then 
begin
      if FileAttrs and faDirectory then{//是不是目录}
    if (sr.Name != "." ) AND (sr.Name != "..")then//
         FindIntheDirectry(sr.Name);//
  else
    FileList.Add(sr.Name);
    end;
    
    while FindNext(sr) = 0 do
     begin
    if FileAttrs and faDirectory then{//是不是目录}
   if (sr.Name != "." ) AND (sr.Name != "..") then//
        FindIntheDirectry(sr.Name);//
else
   FileList.Add(sr.Name);
     end;
      FindClose(sr);
    end;
end;    
    

解决方案 »

  1.   

    我test一下,没有问题一定加分
      

  2.   

    adrianx的代码有问题:如果这个目录下面没有要找的文件,将不会查找子目录!
    在开发中心有我写的一个贴子:轻轻松松找文件--支持回调函数的通用文件查找函数,用我写的那个函数配合你自己的回调函数,可以做任何事情!而且我还给出了一个例子,如果不会用的话,写信给我,我帮你写一个回调函数给你。
      

  3.   

    BOOL FindIntheDirect(LPCTSTR lpDirect)
    {
    char szDirect[MAX_PATH];
    WIN32_FIND_DATA winfd;
    _stprintf(szDirect,_T("%s\\*"),lpDirect);
    HANDLE hFind = FindFirstFile(szDirect,&winfd);
    while(FindNextFile(hFind,&winfd))
    {
    if(winfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
    {
    if(_tcsicmp(winfd.cFileName,_T(".."))!=0&&_tcsicmp(winfd.cFileName,_T("."))!=0)
    {
    char szNextDirect[MAX_PATH];
    _stprintf(szNextDirect,_T("%s\\%s"),lpDirect,winfd.cFileName);
    FindIntheDirect(szNextDirect);
    }
    }
    else
    {
    //所要找的文件...winfd.cFileName
    }
    }
    FindClose(hFind);
    return FALSE;
    }
    这是我的VC代码,是不是我改成Delphi就不对了
      

  4.   

        主要是使用FindFirst()、FindNext()来查找所有的文件:
        Var Path:String;//指定的目录
            SearchRec: TSearchRec;
            Slist: TStringList;
        begin
            Slist:= TStringList.Create();
            if FindFirst(Path,faAnyFile,SearchRec)=0 then 
            begin
               Slist.Add(SearchRec.Name);
               while FindNext(SearchRec)=0 do Slist.Add(SearchRec.Name);
            end;
            FindClose(SearchRec);
        end;
        //这样Slist里就记录了所有的文件名(包括路径);
      

  5.   

    这是我平时写的一个递归函数,检索某路径下的所有文件包括所有的子文件夹的文件,文件被保存到文件中。
    form1中有edit1写入路径。listbox显示文件。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls, FileCtrl, ComCtrls;type
      TForm1 = class(TForm)
        Panel1: TPanel;
        Label1: TLabel;
        Edit1: TEdit;
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Edit1KeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        FNewPath: string;
        procedure DoSearchPathFile(qPath: string);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
      ListBox1.Items.Clear;
      DoSearchPathFile(Edit1.Text);end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Close;
    end;procedure TForm1.DoSearchPathFile(qPath: string);
    var
      SR: TSearchRec;
      FileAttr: Integer;
    begin
      FileAttr := faDirectory;
      FindFirst(qPath  + '\*.*', FileAttr, SR);  While FindNext(SR) = 0 do
      begin
        if (SR.Name <> '.') and (SR.Name <> '..') then begin
          if DirectoryExists(qPath + '\' + SR.Name) then
          begin
            ListBox1.Items.Add('路径'+ qPath + '\' + SR.Name);
            DoSearchPathFile(qPath + '\' + SR.Name);      end;
          //;else ListBox1.Items.Add(qPath + '\' + SR.Name);    end;
      end;  FileAttr := faAnyFile;  FindFirst(qPath + '\*.*', FileAttr, SR);
      while FindNext(SR) = 0 do
      begin
        if (SR.Name <> '.') and (Sr.Name <> '..') then
        begin
          if not(DirectoryExists(qPath + '\' + SR.Name)) then
            ListBox1.Items.Add(qPath + '\' + SR.Name);
        end;
      end;
    end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin  if key = vk_Return then
      begin
        if Trim(Edit1.Text) = '' then
        begin
          ShowMessage('Invalid PathName');
          Exit;
        end;    FNewPath := Edit1.Text[Length(Edit1.Text)];
        if FNewPath = '\' then
          FNewPath := Copy(Edit1.Text, 1, Length(Edit1.Text) - 1)
        else
        begin
          FNewPath := Edit1.Text;
        end;    ListBox1.Clear;
        DoSearchPathFile(FNewPath);  end;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      self.ListBox1.Items.SaveToFile('FileList.txt');
    end;end.
      

  6.   

    SrcPath是路径,应该以“\”结尾;
    IncludeSubDir指定是否包含子目录;
    RetList是返回结果.
    procedure GetFileList(SrcPath: String; IncludeSubDir: Boolean; RetList: TStringList);
    var
      SearchRec: TSearchRec;
    begin
      if (FindFirst(SrcPath+'*.*', faAnyFile, SearchRec)=0) then
      begin
        if (SearchRec.Attr and faDirectory)<>0 then
        begin // 子目录
          if IncludeSubDir and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
          begin
            RetList.Add('Dir='+SrcPath+SearchRec.Name);
            GetFileList(SrcPath + SearchRec.Name + '\', True, RetList);  // 递归查找子目录
          end
        end
        else  // 文件
          RetList.Add('File='+SrcPath+SearchRec.Name);    while FindNext(SearchRec)=0 do
        begin
          if (SearchRec.Attr and faDirectory)<>0 then
          begin // 子目录
          if IncludeSubDir and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
            begin
              RetList.Add('Dir='+SrcPath+SearchRec.Name);
              GetFileList(SrcPath + SearchRec.Name + '\', True, RetList);  // 递归查找子目录
            end
          end
          else  // 文件
            RetList.Add('File='+SrcPath+SearchRec.Name);
        end;
        FindClose(SearchRec);
      end;
    end;