遍历某盘所有目录生成树

来源:互联网 发布:中信证券交易软件 编辑:程序博客网 时间:2024/06/17 20:16

GetRightStr //取右边字符串

 EnumFileInRecursion遍历生成目录树

 

procedure EnumFileInRecursion(ATree :TTreeView; AParentNode :TTreeNode; APath:PChar);
var
  _searchRec :TSearchRec;
  _found, _len :Integer;
  _tmpStr :String;
begin
  //Ctreate RootNode, for example: path is 'C:' or 'C:/'
  _len := Length(APath);
  if _len=2 then //path='C:'
    AParentNode:=ATree.Items.AddFirst(nil, APath[0])
  else if (_len=3) and (APath[2]='/') then //path='C:/',  path is like 'C:f'
    AParentNode:=ATree.Items.AddChild(nil, APath[0]);

  if APath[_len-1] <> '/' then
    _tmpStr := StrPas(APath) + '/*.*'
  else
    _tmpStr := StrPas(APath) + '*.*';

  _found := FindFirst(_tmpStr, faAnyFile, _searchRec);
  while _found=0 do
  begin
    if (_searchRec.Attr and faDirectory)<>0 then
    begin
      if _searchRec.Name = '.' then
        AParentNode:=ATree.Items.AddChild(AParentNode,  GetRightStr('/', StrPas(APath)) );

      if (_searchRec.Name <> '.') and (_searchRec.Name <> '..') then
      begin
        _tmpStr:=StrPas(APath)+'/'+_searchRec.Name;
        EnumFileInRecursion(ATree, AParentNode, PChar(_tmpStr));
      end;
    end;
    _found:=FindNext(_searchRec);
  end;
  FindClose(_searchRec);
end;

 

 

function GetRightStr(ASub, AStr :string):string;
var
  _index :Integer;
begin
  Result := '';
  if AStr = '' then
    Exit;
  _index := Pos(ASub, AStr);
  while _index > 0 do
  begin
    AStr := Copy(AStr, _index + 1, Length(AStr) - _index);
    _index := Pos(ASub, AStr);
  end;
  Result := AStr;
end;

 

经朋友指点:函数GetRightStr可以直接用ExtractFileName代替,效果一样的。学习了,谢谢KONG