1
votes

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?

1

1 Answers

2
votes

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;