title: How can I save a TTreeview to Windows and keep its hierarchical structure?
body: <p>I want to be able to save my Treeview to Windows and keep its hierarchical structure, but cannot seem to get my head around the recursion magic.</p>
  
  <p>Imagine I have two different object types:</p>
  
  <pre><code>TMyFolderNode (represents a folder in the tree)
  TMyItemNode (represents a node containing a stringlist of text for example)
  </code></pre>
  
  <p>The Treeview will be populated at runtime, and for each node added contains a pointer to either one of those objects - typically the <code>TMyFolderNode</code> would represent a parent node for childs of <code>TMyItemNode</code></p>
  
  <p>So basically, if my Treeview looked like this:</p>
  
  <p><img src="https://i.stack.imgur.com/nz8m1.png" alt="enter image description here"></p>
  
  <p>When saving the treeview, the structure in Windows should look like this:</p>
  
  <pre><code>..\Vehicles
  ..\Vehicles\Bikes
  ..\Vehicles\Cars
  ..\Vehicles\Cars\Classic
  ..\Vehicles\Cars\Classic\1996 Shelby Cobra.txt
  ..\Vehicles\Cars\Classic\Corvette Stingray.txt
  ..\Vehicles\Cars\Classic\Jaguar E-Type.txt
  ..\Vehicles\Cars\Tuner
  ..\Vehicles\Cars\Tuner\Toyota Supra RZ.txt
  ..\Vehicles\Trucks
  </code></pre>
  
  <p>This is what I tried:</p>
  
  <pre><code>procedure SaveTreeViewStructure(TreeView: TTreeView; const OutDirectory: string);
  var
    RootDir, SubDir: string;
    Node: TTreeNode;
  
    // the recursive procedure
    procedure SaveNode(Node: TTreeNode);
    var
      Obj: TObject;
      Child: TTreeNode;
    begin
      Obj := TObject(Node.Data);
  
      // node type is a folder/parent, so create the folder in windows...
      if Obj.ClassType = TMyFolderNode then
      begin
        ForceDirectories(RootDir + '\' + Node.Text);
      end;
  
      // node is a type holding data we can save to text file...
      if Obj.Create.ClassType = TMyItemNode then
      begin
        with TMyItemNode(Obj) do
        begin
          // save the file as .txt...
        end;
      end;
  
      Child := Node.GetFirstChild;
      while Assigned(Child) do
      begin
        SaveNode(Child);
        Child := Child.GetNextSibling;
      end;
    end;
  
  begin
    RootDir   := OutDirectory + '\';
    SubDir    := '';
    Node      := nil;
  
    if ForceDirectories(RootDir) then // make sure base folder exists first
    begin
      Node := TreeView.Items[0]; // start from the first tree node (TopItem causes problems)
      while Assigned(Node) do // until node is no longer assigned
      begin
        SaveNode(Node); // call the recursive procedure
        Node := Node.GetNextSibling; // go to the next node
      end;
    end;
  end;
  </code></pre>
  
  <p>If I try saving just the <code>TMyFolderNode</code> nodes they dont get created in the proper child folders etc, and then when I try saving the text <code>TMyItemNode</code> I get cannot find file errors (likely because the folders are not saving properly).</p>
  
  <p>Clearly I dont fully understand the logic behind recursion, I know it means to call itself until a condition is met (eg we reached the last node in the tree), but I cannot seem to figure this one out.</p>
  
  <p>So how do I save my Treeview to a base folder in Windows, where the recursive procedure would create the required sub directories and save the regular node items in the correct place?</p>
  
  <p>Is my approach here wrong? I did try first with <code>for loops</code> but the folders kept doubling up and other weird things.</p>
  
  <p>Many thanks in advance.</p>