给你两个函数,这是我以前做的,都可以实现:(可以将Memo换成字符串列表)
procedure TForm1.FindAllFiles(var Path: string);
var
Sr: TSearchRec;
Err : Integer;
Oldlen : Integer;
RecPath:string;
begin
RecPath:=Path;
Oldlen := Length(Path);
Err := FindFirst( Path+'*.*',$37,Sr);
while Err=0 Do
begin
If (Sr.Attr and (faDirectory or faVolumeID)) = 0 Then //是文件
begin
Memo1.Lines.Add(Path+Sr.Name);
end;
If ((Sr.Attr and faDirectory)<>0) and (Sr.Name[1] <> '.')Then //是目录
begin
Memo1.Lines.Add(Path+Sr.Name);// ExpandFileName(Sr.Name)
RecPath:= RecPath + Sr.Name + '/';
FindAllFiles(RecPath);
Delete(Recpath,Oldlen+1,256); //
End;
Err := FindNext(Sr);
End;
FindClose(Sr);
end;
procedure TForm1.MakeTree;
var Sr : TSearchRec;
Err : integer;
//FilePath : string;
QuotationIn:Boolean;
begin
Err:=FindFirst('*.*',$37,Sr) ;
While (Err = 0) do
begin
QuotationIn:=True;
if Sr.Name[1]<>'.' then // 如果不是目录
begin
if Pos('''',Sr.Name)<>0 then QuotationIn:=False;
if ((Sr.Attr and faDirectory)=0) and QuotationIn then
begin
Memo2.Lines.Add(ExpandFileName(Sr.Name));
end
end;
if ((Sr.Attr and faDirectory)<>0) and (Sr.Name[1] <> '.') and QuotationIn then //如果是目录
begin
Memo2.Lines.Add(ExpandFileName(Sr.Name));
ChDir(Sr.Name) ;//ChDir(ExpandFileName(Sr.Name))
MakeTree;
ChDir('..') ;
end ;
Err:=FindNext(Sr) ;
Application.ProcessMessages;
end ;
FindClose(Sr);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Dir:string;
begin
Memo1.Lines.Clear;
if SelectDirectory('请选择光盘的盘符或一个目录:','',Dir) then
if Dir='' then Exit
else
if Dir[Length(Dir)]<>'/' then
begin
Dir:=Dir+'/';
ChDir(Dir);
FindAllFiles(Dir);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Dir:string;
begin
Memo2.Lines.Clear;
if SelectDirectory('请选择光盘的盘符或一个目录:','',Dir) then
if Dir='' then Exit
else
if Dir[Length(Dir)]<>'/' then
begin
Dir:=Dir+'/';
ChDir(Dir);
MakeTree;
end;
end;