给你一个完整的递归函数,可以在TTREEVIEW中作为目录树,试试
procedure ImportDirToTreeView(var Dir,Mask:string;ParentNode:TTreeNode);
var
SRec: TSearchRec;
ANode:TTreeNode;
Path,Fn,Fx,TitleStr:string;
retval:integer;
begin
Path:=Dir; //搜索路径
retval := FindFirst( Dir+Mask,faAnyFile,SRec); //开始
While retval=0 Do
Begin
if IsBreakWhile then Break;
If (SRec.Attr and (faDirectory or faVolumeID)) = 0 Then //是文件
begin
Application.ProcessMessages;
Fn:=SRec.Name; //取文件名
TitleStr :=Fn;
Fx :=UpperCase(ExtractFileExt(Fn)); //取扩展名
Delete(TitleStr,Length(TitleStr)-3,4); //取文件前名称
ANode :=FileImportTreeViewBookTitle(ParentNode,TitleStr,Fx); //建立TREEVIEW标题
end;
retval := FindNext(SRec); //找下一个
End;
SysUtils.FindClose(SRec);
retval:=FindFirst(path+'*.*',faDirectory,SRec); //目录搜索
While retval=0 Do
Begin
if IsBreakWhile then Break;
If (SRec.Attr and faDirectory)<>0 Then //是目录
If (SRec.Name <> '.') and (SRec.Name <> '..') Then
Begin
Application.ProcessMessages;
Path:=Path+SRec.Name+'\'; //新路径
Fn:=SRec.Name; //取文件名
TitleStr :=Fn;
Fx :=UpperCase(ExtractFileExt(Fn)); //取扩展名
Delete(TitleStr,Length(TitleStr)-3,4); //取文件前名称
ANode :=FileImportTreeViewBookTitle(ParentNode,TitleStr,'DIR'); //建立TREEVIEW标题
ImportDirToTreeView(path,mask,ANode); //当前标题为主标题递归
End;
retval := FindNext(SRec); //下一个
End;
SysUtils.FindClose(SRec);
end;