• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi两个遍历指定目录下指定类型文件的函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
// ================================================================
// 遍历某个文件夹下某种文件,
// 使用说明
//  _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;

 

 

// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
//  _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;

 

SysUtils.FindClose(FileRec);

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

 

TmpList.Free;

end;

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
数学软件实训1-MATLAB程序设计及应用初步发布时间:2022-07-18
下一篇:
matlab常用的一些程序和功能 - 悟江居士发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap