delphi遍历指定目录下指定类型文件的函数


本文整理自网络,侵删。

 遍历指定目录下指定类型文件的函数
// ================================================================
// 遍历某个文件夹下某种文件,
// 使用说明
// ??G _GetFileList(ListBox1.Items,'c:\*.doc');
// _GetFileList(MyTStringList,'c:\*.exe');
// ================================================================
procedure TForm1._GetFileList(AStrings: TStrings ; ASourFile: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec:TSearchrec;
begin

sour_path:=ExtractFilePath(ASourFile);
sour_file:=ExtractFileName(ASourFile);

if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;

TmpList:=TStringList.Create;
TmpList.Clear;

if FindFirst(sour_path+sour_file,faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) = 0) then
begin
TmpList.Add(sour_path+FileRec.Name)
end;
until FindNext(FileRec)<>0;

SysUtils.FindClose(FileRec);

AStrings.Assign(TmpList);

TmpList.Free;
end;

// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
// ??G _GetFileList(ListBox1.Items, 'c:\', '*.doc');
// _GetFileList(MyTStringList, 'c:\', '*.exe');
// ================================================================
procedure _GetFileList(AStrings: TStrings; ASourFile,
FileName: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec, subFileRec:TSearchrec;
i: Integer;
begin
if rightStr(trim(ASourFile), 1) <> '\' then
sour_path :=trim(ASourFile) + '\'
else
sour_path :=trim(ASourFile);
sour_file:= FileName;

if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;

TmpList:=TStringList.Create;
TmpList.Clear;

if FindFirst(sour_path+'*.*',faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name<> '.') and (FileRec.Name <> '..')) then
_GetFileList(AStrings, sour_path+ FileRec.Name + '\', sour_file);
end
else
if FindFirst(sour_path + FileName,faAnyfile,subFileRec) = 0 then
repeat
if ((subFileRec.Attr and faDirectory) = 0) then
TmpList.Add(sour_path+subFileRec.Name);
until FindNext(subFileRec)<>0;

until FindNext(FileRec)<>0;

FindClose(FileRec);
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);

TmpList.Free;
end;

相关阅读 >>

Delphi 关于多语言:如何获取当前系统语言

Delphi 把字符串复制到剪贴板

Delphi xe2读取内存偏移数据代码

Delphi xe3里判断网络是否连接成功

Delphi 根据字符串找到函数并执行

Delphi时间格式错误

Delphi unigui 获取files路径

Delphi的tservice 服务路径获取 dll中获取文件路径

Delphi时间相减函数

Delphi playsound()函数应用

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



打赏

取消

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

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

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

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

评论

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