LAZARUS遍歷文件夾/文件

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.

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.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章