How to Search a File through all the SubDirectories in Delphi

前端 未结 5 1983
悲&欢浪女
悲&欢浪女 2020-12-19 15:02

I implemented this code but again i am not able to search through the subdirectories .

     procedure TFfileSearch.FileSearch(const dirName:string);
     beg         


        
5条回答
  •  心在旅途
    2020-12-19 15:18

    procedure FindFilePattern(root:String;pattern:String);
    var
      SR:TSearchRec;
    begin
      root:=IncludeTrailingPathDelimiter(root);
      if FindFirst(root+'*.*',faAnyFile,SR) = 0 then
      begin
          repeat
              Application.ProcessMessages;
              if ((SR.Attr and faDirectory) = SR.Attr ) and (pos('.',SR.Name)=0) then
                 FindFilePattern(root+SR.Name,pattern)
              else
              begin
               if pos(pattern,SR.Name)>0 then Form1.ListBox1.Items.Add(Root+SR.Name);
              end;
          until FindNext(SR)<>0;
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      FindFilePattern('C:\','.exe');
    end;
    

    This searches recursively to all folders displaying filenames that contain a certain pattern.

提交回复
热议问题