procedure MakeFileList(Path,FileExt:string;var List:TStringList);
//Path为需要遍历的文件目录。最后有斜杠无斜杠都可以
//FileExt后缀名要加点 如 '.exe'
//List为找到的文件的路径地址列表
//2012-12-13 一路随云
var
sch:TSearchrec;
begin
if List=nil then List:=TStringlist.Create;
Path:=Trim(Path);
if Path[Length(Path)] <> '\' then Path := Path+ '\';
if not DirectoryExists(Path) then
begin
List.Clear;
exit;
end;
if FindFirst(Path + '*', faAnyfile, sch) = 0 then
begin
repeat
Application.ProcessMessages;
if ((sch.Name = '.') or (sch.Name = '..')) then Continue; //如果找到的是当前目录 就继续下一次查找
if DirectoryExists(Path+sch.Name) then //如果某个目录存在,则进入这个目录递归找到文件
begin
MakeFileList(Path+sch.Name,FileExt,List);
end
else
begin
if (UpperCase(extractfileext(Path+sch.Name)) = UpperCase(FileExt)) or (FileExt='.*') then
List.Add(Path+sch.Name);
end;
until FindNext(sch) <> 0;
SysUtils.FindClose(sch);
end;
end;
DELPHI 递归遍历文件代码
最新推荐文章于 2019-01-08 00:45:49 发布