在我的应用程序中,我使用以下过程递归扫描任何文件夹和子文件夹,如果文件夹包含文本文件(*.txt)我将文件名添加到我的过程中定义的TStringList:
procedure FileSearch(const PathName: string; var lstFiles: TStringList);
const
FileMask = '*.txt';
var
Rec: TSearchRec;
Path: string;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileMask, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
lstFiles.Add(Path + Rec.Name);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and
(Rec.Name <> '..') then
FileSearch(Path + Rec.Name, lstFiles);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
end;
Run Code Online (Sandbox Code Playgroud)
一切都很完美,但我希望能够搜索多个文件扩展名.我已经尝试修改FileMask来执行此操作但每次都不返回任何内容,可能是因为它正在寻找无效的扩展.我已经尝试了以下各项而没有运气:(一次尝试过一次,我的程序中没有写下3行以下)
FileMask = '*.txt|*.rtf|*.doc';
FileMask = '*.txt;*.rtf;*.doc';
FileMask = '*.txt,*.rtf,*.doc';
Run Code Online (Sandbox Code Playgroud)
我对此问题感到愚蠢,但如何将额外的文件扩展名包含在搜索中呢?我可以为打开和保存对话框执行此操作,为什么我不能在此处分隔扩展?
谢谢.
克雷格.
Ken*_*ite 12
更改您的函数,以便它也接受扩展名列表,用分号或其他分隔符分隔.然后,您可以在该扩展列表中检查每个找到的文件扩展名是否存在,如果找到,则将其添加到您的字符串列表中.
这样的事情应该有效:
procedure FileSearch(const PathName: string; const Extensions: string;
var lstFiles: TStringList);
const
FileMask = '*.*';
var
Rec: TSearchRec;
Path: string;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileMask, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
if AnsiPos(ExtractFileExt(Rec.Name), Extensions) > 0 then
lstFiles.Add(Path + Rec.Name);
until FindNext(Rec) <> 0;
finally
SysUtils.FindClose(Rec);
end;
if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and
(Rec.Name <> '..') then
FileSearch(Path + Rec.Name, Extensions, lstFiles);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
end;
Run Code Online (Sandbox Code Playgroud)
示例电话:
FileSearch('C:\Temp', '.txt;.tmp;.exe;.doc', FileList);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12167 次 |
| 最近记录: |