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