您当前的位置:首页 > 建站知识 > 编程知识

Delphi 取指定文件夹下所有文件

发布时间: 2013-08-15 │ 浏览:3342 

取指定文件夹下所有文件的代码:

unit Unit1; 

interface 

uses 
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
StdCtrls; 

type 
TForm1 = class(TForm) 
Button1: TButton; 
Memo1: TMemo; 
procedure Button1Click(Sender: TObject); 
private 
{ Private declarations } 
public 
{ Public declarations } 
end; 

var 
Form1: TForm1; 

implementation 
var 
stl: TStringList; 

{$R *.DFM} 

procedure FindFiles(APath, AFile: string;AStl:TStringList); 
var 
FindResult: integer; 
FSearchRec, DSearchRec: TSearchRec; 
function IsDirNotation(ADirName: string): Boolean; 
begin 
Result := ((ADirName = '.') or (ADirName = '..')); 
end; 
begin 
if APath[Length(APath)] <> '\' then 
APath := APath + '\'; 
FindResult := FindFirst(APath + AFile, faAnyFile + faHidden +faSysFile + faReadOnly, FSearchRec); //在根目录中查找指定文件 
try 
while FindResult = 0 do 
begin 
AStl.Add(APath + FSearchRec.Name); 
FindResult := FindNext(FSearchRec); // 查找下一个指定文件 
end; 
FindResult := FindFirst(APath + '*.*', faDirectory, DSearchRec); //进入当前目录的子目录继续查找 
while FindResult = 0 do 
begin 
if ((DSearchRec.Attr and faDirectory) = faDirectory) and not IsDirNotation(DSearchRec.Name) then 
FindFiles(APath + DSearchRec.Name, AFile,AStl); //递归调用FindFiles函数 
FindResult := FindNext(DSearchRec); 
end;