如何通过Delphi中的所有子目录搜索文件 [英] How to Search a File through all the SubDirectories in Delphi
本文介绍了如何通过Delphi中的所有子目录搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
If FindFirst filePath,faAnyFile,searchResult)= 0然后
尝试
重复
lbSearchResult.Items.Append(searchResult.Name);
直到FindNext(searchResult)<> 0
除了
在e:Exception do
ShowMessage(e.Message);
结束// try ends
FindClose(searchResult);
解决方案
如果你不需要线程,最简单的方法这是:
程序TForm1.AddAllFilesInDir(const Dir:string);
var
SR:TSearchRec;
begin
如果FindFirst(IncludeTrailingBackslash(Dir)+'*。*',faAnyFile或faDirectory,SR)= 0然后
try
repeat
if(SR。 Attr和faDirectory)= 0然后
ListBox1.Items.Add(SR.Name)
else if(SR.Name<>'。')和(SR.Name& '')然后
AddAllFilesInDir(IncludeTrailingBackslash(Dir)+ SR.Name); //递归调用!
until FindNext(Sr)< 0;
finally
FindClose(SR);
结束
结束
procedure TForm1.Button1Click(Sender:TObject);
begin
ListBox1.Items.BeginUpdate;
AddAllFilesInDir('C:\Users\Andreas Rejbrand\Documents\Aweb');
ListBox1.Items.EndUpdate;
结束
I have implemented this code in Delphi, it will search for the File or the name given but it omits searching all the subdirectories. How can this be done?
Code:
if FindFirst(filePath,faAnyFile,searchResult)=0 then
try
repeat
lbSearchResult.Items.Append(searchResult.Name);
until FindNext(searchResult)<>0
except
on e:Exception do
ShowMessage(e.Message);
end; //try ends
FindClose(searchResult);
解决方案
If you don't need threading, the simplest way is this:
procedure TForm1.AddAllFilesInDir(const Dir: string);
var
SR: TSearchRec;
begin
if FindFirst(IncludeTrailingBackslash(Dir) + '*.*', faAnyFile or faDirectory, SR) = 0 then
try
repeat
if (SR.Attr and faDirectory) = 0 then
ListBox1.Items.Add(SR.Name)
else if (SR.Name <> '.') and (SR.Name <> '..') then
AddAllFilesInDir(IncludeTrailingBackslash(Dir) + SR.Name); // recursive call!
until FindNext(Sr) <> 0;
finally
FindClose(SR);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.BeginUpdate;
AddAllFilesInDir('C:\Users\Andreas Rejbrand\Documents\Aweb');
ListBox1.Items.EndUpdate;
end;
这篇关于如何通过Delphi中的所有子目录搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文