如何通过Delphi中的所有子目录搜索文件 [英] How to Search a File through all the SubDirectories in Delphi

查看:114
本文介绍了如何通过Delphi中的所有子目录搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 程序TFfileSearch.FileSearch(const dirName)

我实现了这个代码,但是我再次无法搜索子目录。 :串);
begin
//我们在这里写我们的搜索代码
如果FindFirst(dirName,faAnyFile或faDirectory,searchResult)= 0然后
开始
尝试
重复
ShowMessage(IntToStr(searchResult.Attr));
if(searchResult.Attr and faDirectory)= 0 then //结果是一个文件
// begin
lbSearchResult.Items.Append(searchResult.Name)
else
begin
FileSearch(IncludeTrailingBackSlash(dirName)+ searchResult.Name);
//
end;
直到FindNext(searchResult)0
finally
FindClose(searchResult);
结束
结束
结束
程序TFfileSearch.btnSearchClick(Sender:TObject);
var
filePath:string;
begin
lbSearchResult.Clear;
如果修剪(edtMask.Text)=''然后
MessageDlg('EMPTY INPUT',mtWarning,[mbOK],0)
else
begin
filePath: = cbDirName.Text + edtMask.Text;
ShowMessage(filePath);
FileSearch(filePath);

end;

结束;



在E:\驱动器中搜索* .ini文件。所以最初的filePath是E:*。ini。
但代码不搜索E:\驱动器中的目录。如何纠正?



感谢提前

解决方案

你可以在 FindFirst 的调用中对文件扩展名应用限制。如果你这样做,那么目录不会被枚举。相反,您必须在代码中检查匹配的扩展名。尝试这样的东西:

 程序TMyForm.FileSearch(const dirName:string); 
var
searchResult:TSearchRec;
begin
如果FindFirst(dirName +'\ *',faAnyFile,searchResult)= 0,则开始
try
repeat
if(searchResult.Attr和faDirectory)= 0然后开始
如果SameText(ExtractFileExt(searchResult.Name),'.ini')然后开始
lbSearchResult.Items.Append(IncludeTrailingBackSlash(dirName)+ searchResult.Name);
结束
end else if(searchResult.Name<>'。')和(searchResult.Name&';''')然后开始
FileSearch(IncludeTrailingBackSlash(dirName)+ searchResult.Name);
结束
直到FindNext(searchResult)0
finally
FindClose(searchResult);
结束
结束
结束

procedure TMyForm.FormCreate(Sender:TObject);
begin
FileSearch('c:\windows');
结束


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

     procedure TFfileSearch.FileSearch(const dirName:string);
     begin
//We write our search code here
  if FindFirst(dirName,faAnyFile or faDirectory,searchResult)=0 then
  begin
    try
      repeat
      ShowMessage(IntToStr(searchResult.Attr));
        if (searchResult.Attr and faDirectory)=0 then   //The Result is a File
        //begin
          lbSearchResult.Items.Append(searchResult.Name)
         else 
         begin
            FileSearch(IncludeTrailingBackSlash(dirName)+searchResult.Name);
           //
         end;
       until FindNext(searchResult)<>0
     finally
     FindClose(searchResult);
     end;
   end;
   end;
    procedure TFfileSearch.btnSearchClick(Sender: TObject);
   var
 filePath:string;
begin
lbSearchResult.Clear;
if Trim(edtMask.Text)='' then
  MessageDlg('EMPTY INPUT', mtWarning, [mbOK], 0)
else
begin
  filePath:=cbDirName.Text+ edtMask.Text;
  ShowMessage(filePath);
  FileSearch(filePath);

end;

end;

I am giving the search for *.ini files in E:\ drive. so initially filePath is E:*.ini. But the code does not search the directories in E:\ drive. How to correct it?

Thanks in Advance

解决方案

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;

这篇关于如何通过Delphi中的所有子目录搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆