妖の僧的 Delphi 博客

记录学习过程中的点点滴滴,是喜欢,不是职业;记性不好,特别需要这么一个博客.

导航

遍历指定目录下指定类型文件的函数(fatkun Fix版)

// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
// _GetFileList(ListBox1.Items, 'c:\', '.doc');
// _GetFileList(MyTStringList, 'c:\', '.exe');
// 这不是万一博客的原版咧,是1L的fatkun修复的一版,且不管看不看得懂,收藏个先

// ================================================================
procedure _GetFileList(AStrings: TStrings; ASourFile,FileName: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec, subFileRec:TSearchrec;
i: Integer;
begin
if copy(ASourFile,length(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 begin
if (Pos(sour_file,sour_path+FileRec.Name)>0) then
TmpList.Add(sour_path+FileRec.Name);
end;

until FindNext(FileRec)<>0;

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

TmpList.Free;
end;

posted on 2015-03-14 16:20  妖の僧  阅读(254)  评论(0)    收藏  举报