Delphi中如何通过所有子目录搜索文件
我实现了这段代码,但我再次无法搜索子目录。
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;
结尾;
我正在 E:\ 驱动器中搜索 *.ini 文件。所以最初的 filePath 是 E:*.ini。 但代码不会搜索 E:\ 驱动器中的目录。如何纠正呢?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法在调用
FindFirst
时对文件扩展名应用限制。如果您这样做,则不会枚举目录。相反,您必须检查代码中是否有匹配的扩展名。尝试这样的事情: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:这是一种更现代的方法,可以消除
FindFirst
/FindNext
之间的麻烦:Here is a more modern approach that gets rid of the headache between
FindFirst
/FindNext
:我讨厌那些使用 FindFirst/FindNext 的递归解决方案,我认为这很麻烦,有些人甚至忘记使用 FindClose 来清理资源。因此,为了它的乐趣,一个应该实用的非递归解决方案......
虽然它似乎因为使用动态数组而占用大量内存,但递归方法将执行完全相同的操作,但递归发生在堆栈!此外,使用递归方法时,会为所有局部变量分配空间,而我的解决方案仅为文件夹名称分配空间。
当您检查速度时,两种方法应该一样快。不过,递归方法更容易记住。您还可以使用 TStringList 代替动态数组,但我只是喜欢动态数组。
我的解决方案还有一个技巧:它可以在多个文件夹中搜索!我只用一个根初始化了文件夹数组,但您可以轻松地将其长度设置为 3,并将文件夹[0] 设置为 C:\,将文件夹[1] 设置为 D:\,将文件夹[2] 设置为 E:\,然后就可以了将在多个磁盘上搜索!
顺便说一句,将 WriteLn() 代码替换为您想要执行的任何逻辑...
I hate those recursive solutions with FindFirst/FindNext and I consider it troublesome that some even forget to use FindClose to clean up resources. So, for the fun of it, a non-recursive solution that should be practical to use...
While it seems to eat a lot of memory because it uses a dynamic array, a recursive method will do exactly the same but recursion happens on the stack! Also, with a recursive method, space is allocated for all local variables while my solution only allocates space for the folder names.
When you check for speed, both methods should be just as fast. The recursive method is easier to remember, though. You can also use a TStringList instead of a dynamic array, but I just like dynamic arrays.
One additional trick with my solution: It can search in multiple folders! I Initialized the Folders array with just one root, but you could easily set it's length to 3, and set Folders[0] to C:\, Folders[1] to D:\ and Folders[2] to E:\ and it will search on multiple disks!
Btw, replace the WriteLn() code with whatever logic you want to execute...
这对我来说适用于多扩展搜索支持:
用法:
This is worked for me with multi-extension search support:
Usage:
这个文件搜索的问题是它会无限循环,FindClose就像不存在一样。
The problem with this file search is that it will loop infinitely, FindClose is like it does not exist.
这会递归搜索显示包含特定模式的文件名的所有文件夹。
This searches recursively to all folders displaying filenames that contain a certain pattern.