算法如下所示:
procedure RecursionForeachAllFiles(var ACount :Integer; AMemo :TMemo; AFilePath :String);
//参数说明:
//ACount :用于计算所遍历得到的文件数
//AMemo :用于显示所遍历得到的文件名
//AFilePath :所要遍历的目录
var
_SearchRec :TSearchRec;
_Finded :Integer;
_Str :string;
begin
if AFilePath[Length(AFilePath)] <> '/' then
AFilePath := AFilePath + '/';
_Finded := FindFirst(AFilePath + '*.*', faAnyFile, _SearchRec);
while _Finded = 0 do
begin
if (_SearchRec.Attr and faDirectory) <> 0 then //如果为文件夹则调用自己递归去找
begin
if (_SearchRec.Name <> '.') and (_SearchRec.Name <> '..') then
RecursionForeachAllFiles(ACount, AMemo, AFilePath +_SearchRec.Name);
end
else
begin
AMemo.Lines.Add(AFilePath + '' + _SearchRec.Name);
inc(ACount);
end;
_Finded := FindNext(_SearchRec);
end;
FindClose(_SearchRec);
end;
名词解释:
Recursion:递归
Foreach:遍历