How to Search a File through all the SubDirectories in Delphi

前端 未结 5 1986
悲&欢浪女
悲&欢浪女 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:16

    You can't apply a restriction to the file extension in the call to FindFirst. If you did so then directories do not get enumerated. Instead you must check for matching extension in your code. Try something like this:

    procedure TMyForm.FileSearch(const dirName:string);
    var
      searchResult: TSearchRec;
    begin
      if FindFirst(dirName+'\*', faAnyFile, searchResult)=0 then begin
        try
          repeat
            if (searchResult.Attr and faDirectory)=0 then begin
              if SameText(ExtractFileExt(searchResult.Name), '.ini') then begin
                lbSearchResult.Items.Append(IncludeTrailingBackSlash(dirName)+searchResult.Name);
              end;
            end else if (searchResult.Name<>'.') and (searchResult.Name<>'..') then begin
              FileSearch(IncludeTrailingBackSlash(dirName)+searchResult.Name);
            end;
          until FindNext(searchResult)<>0
        finally
          FindClose(searchResult);
        end;
      end;
    end;
    
    procedure TMyForm.FormCreate(Sender: TObject);
    begin
      FileSearch('c:\windows');
    end;
    

提交回复
热议问题