天天看点

LAZARUS遍历文件夹/文件1.遍历所有文件夹。2.遍历文件。

1.遍历所有文件夹。

program getfiles;
uses Classes,sysutils;
var sPath:string;
  L:TStringList;
procedure getdir(path:string; var List:TStringList);//函数的具体描述
var
  SR:TSearchRec;
  i:smallint;
begin
  List.Add(Utf8ToAnsi(path));
  i:=0;
  while i<List.Count do
  begin
  if FindFirst(List[i] + '\*', faAnyFile, SR) = 0 then
  begin
       repeat
             if (SR.Name<>'.') and (SR.Name<>'..') and (SR.Attr=faDirectory) then
             List.Add(List[i]+'\'+SR.Name);
       until FindNext(SR) <> 0;
       FindClose(SR);
  end;
   inc(i);
  end;
end;
begin
  sPath:='D:\MyDrivers';
  L:=TStringList.Create;
  getdir(sPath,L);
  Writeln(L.Text);
  readln;
end.
           
LAZARUS遍历文件夹/文件1.遍历所有文件夹。2.遍历文件。

2.遍历文件。

program Project1;
uses Classes,sysutils;
var sPath:string;
  FL:TStringList;
procedure getdir(path:string; var FileList:TStringList);//函数的具体描述
var
  SR:TSearchRec;
  i:smallint;
  List:TStringList;
begin
  List:=TStringList.Create;
  List.Add(Utf8ToAnsi(path));
  i:=0;
  while i<List.Count do
  begin
  if FindFirst(List[i] + '\*', faAnyFile, SR) = 0 then
  begin
       repeat
             if (SR.Name<>'.') and (SR.Name<>'..') and (SR.Attr=faDirectory) then
             List.Add(List[i]+'\'+SR.Name)
             else
                if  (SR.Name<>'.') and (SR.Name<>'..') and (SR.Attr=faArchive) then
                 FileList.Add(List[i]+'\'+SR.Name);
       until FindNext(SR) <> 0;
       FindClose(SR);
  end;
   inc(i);
  end;
end;
begin
  sPath:='E:\迅雷下载\SQLCipher_v4.3.0(解压密码:123456)';
  FL:=TStringList.Create;
  getdir(sPath,FL);
  Writeln(FL.Text);
  readln;
end.
           
LAZARUS遍历文件夹/文件1.遍历所有文件夹。2.遍历文件。