DELPHI遍历文件目录


本文整理自网络,侵删。

 procedure GetFileListEx(FilePath, ExtMask: string; FileList: TStrings; SubDirectory: Boolean = True); //遍历目录及子目录
function Match(FileName: string; MaskList: TStrings): boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to MaskList.Count - 1 do
begin
if MatchesMask(FileName, MaskList[i]) then
begin
Result := True;
break;
end;
end;
end;
var
FileRec: TSearchrec;
MaskList: TStringList;
begin
if DirectoryExists(FilePath) then
begin
if FilePath[Length(FilePath)] <> '\' then FilePath := FilePath + '\';
if FindFirst(FilePath + '*.*', faAnyfile, FileRec) = 0 then
begin
MaskList := TStringList.Create;
try
ExtractStrings([';'], [], PChar(ExtMask), MaskList);
FileList.BeginUpdate;
repeat
if ((FileRec.Attr and faDirectory) <> 0) and SubDirectory then
begin
if (FileRec.Name <> '.') and (FileRec.Name <> '..') then
GetFileListEx(FilePath + FileRec.Name + '\', ExtMask, FileList);
end
else
begin
if Match(FilePath + FileRec.Name, MaskList) then
FileList.Add(FilePath + FileRec.Name);
end;
until FindNext(FileRec) <> 0;
FileList.EndUpdate;
finally
MaskList.Free;
end;
end;
FindClose(FileRec);
end;
end;

相关阅读 >>

如何在Delphi中获取字符串字节数?

Delphi 正则表达式校验手机号

Delphi中将webbrowser用作网页编辑器

Delphi 屏幕dpi计算

Delphi切换指定窗口到最前并获得焦点

Delphi 中tresourcestream流使用

Delphi tadodataset 中文使用说明

Delphi copyrect实现的几个图片的转换效果

Delphi 隐藏桌面图标和任务栏

Delphi10.3通过rest单元使类和json数据互相转换

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...