1

I have stuck with this problem.

I have a list of filenames from an archive (here are files and folders). This list looks like this :

folder
folder/index.html
otherfolder

The problem is to make the hierarchy into the virtualstringtree. Files and folders that belongs will be the child nodes from a root folder.

Any ideas?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Dan Godiac
  • 11
  • 1

1 Answers1

2

Here is a full code. You read line, you split this line to get each subfolder. You search for each subfolder if it's already in the tree, if not you create the new subfolder. Tested with a classic treeview :)

procedure TForm1.Analyze(ListOfFiles : TStringList);
var
   root: TTreeNode;
   nI: Integer;
   files : TStringList;
  nJ: Integer;
begin
   for nI := 0 to ListOfFiles.Count - 1 do
   begin
        files := TStringList.Create;
        files.Delimiter := '/';
        files.DelimitedText := ListOfFiles[nI];
        root := nil;
        for nJ := 0 to files.Count - 1 do
             root := GetFolder(root, files[nJ])

        FreeAndNil( Files );
   end;
end;

function TForm1.GetFolder( TreeNode : TTreeNode; SubFolder : String ) : TTreeNode;
var
  nI: Integer;
begin
     result := nil;

     if Assigned( TreeNode ) then
     begin
         for nI := 0 to TreeNode.Count - 1 do
         begin
              if SameText(TreeNode.Item[nI].Text, SubFolder) then
              begin
                 result := TreeNode.item[nI];
                 Exit;
              end;
         end;
     end
     else
     begin
         for nI := 0 to TreeView1.Items.Count - 1 do
         begin
              if SameText(TreeView1.Items[nI].Text, SubFolder) then
              begin
                 result := TreeView1.Items[nI];
                 Exit;
              end;
         end;

     end;
     if not Assigned( result ) then
     begin
          result := TreeView1.Items.AddChild( TreeNode, SubFolder );
          Exit;
     end;

end;
Greg M.
  • 229
  • 1
  • 9
  • Hy. This code works perfectly on a treeview. How can i integrate it to virtualstringtree? – Dan Godiac Jul 01 '13 at 09:47
  • TTreeNode should be changed to PVirtualNod, Replace Treeview1 with your VirtualTreeview. I don't have a delphi with VirtualTreeview installed. But, it's quite easy i think. Search a little bit ;) – Greg M. Jul 01 '13 at 12:10