Recurse through TreeView and create folders












0














I want to build a function to recurse through a treeview and create each folder on a harddrive. The code is working fine so far. Once it reaches the end of a node it jumps one level deeper. But whta I am missing here is a line of code to jump back to the upper folder to continue creating the other folders.
This is what I have so far:



    Private Sub RecurseNodes(ByVal col As TreeNodeCollection)
For Each tn As TreeNode In col
Console.WriteLine(drive_letter & treefix & tn.Text)
If tn.Nodes.Count > 0 Then
treefix = treefix & tn.Text & ""
RecurseNodes(tn.Nodes)
End If
Next tn
End Sub


This is what my output looks like:






E:Auftraggeber Projektname 00000
E:Auftraggeber Projektname 00000Export
E:Auftraggeber Projektname 00000Material
E:Auftraggeber Projektname 00000MaterialOriginal
E:Auftraggeber Projektname 00000MaterialTranscodiert
E:Auftraggeber Projektname 00000MaterialFootage
E:Auftraggeber Projektname 00000MaterialGFX
E:Auftraggeber Projektname 00000MaterialSFX
E:Auftraggeber Projektname 00000MaterialSprecher
E:Auftraggeber Projektname 00000MaterialProjekt
E:Auftraggeber Projektname 00000MaterialMusik





and this is what the tree actually looks like:
TreeView



So once the code gets to E:Auftraggeber Projektname 00000MaterialFootage, it should go back the upper folder to continue processing.



It is probably just a single line or two of code but I just can't figure it out.
Thanks for your help!










share|improve this question



























    0














    I want to build a function to recurse through a treeview and create each folder on a harddrive. The code is working fine so far. Once it reaches the end of a node it jumps one level deeper. But whta I am missing here is a line of code to jump back to the upper folder to continue creating the other folders.
    This is what I have so far:



        Private Sub RecurseNodes(ByVal col As TreeNodeCollection)
    For Each tn As TreeNode In col
    Console.WriteLine(drive_letter & treefix & tn.Text)
    If tn.Nodes.Count > 0 Then
    treefix = treefix & tn.Text & ""
    RecurseNodes(tn.Nodes)
    End If
    Next tn
    End Sub


    This is what my output looks like:






    E:Auftraggeber Projektname 00000
    E:Auftraggeber Projektname 00000Export
    E:Auftraggeber Projektname 00000Material
    E:Auftraggeber Projektname 00000MaterialOriginal
    E:Auftraggeber Projektname 00000MaterialTranscodiert
    E:Auftraggeber Projektname 00000MaterialFootage
    E:Auftraggeber Projektname 00000MaterialGFX
    E:Auftraggeber Projektname 00000MaterialSFX
    E:Auftraggeber Projektname 00000MaterialSprecher
    E:Auftraggeber Projektname 00000MaterialProjekt
    E:Auftraggeber Projektname 00000MaterialMusik





    and this is what the tree actually looks like:
    TreeView



    So once the code gets to E:Auftraggeber Projektname 00000MaterialFootage, it should go back the upper folder to continue processing.



    It is probably just a single line or two of code but I just can't figure it out.
    Thanks for your help!










    share|improve this question

























      0












      0








      0







      I want to build a function to recurse through a treeview and create each folder on a harddrive. The code is working fine so far. Once it reaches the end of a node it jumps one level deeper. But whta I am missing here is a line of code to jump back to the upper folder to continue creating the other folders.
      This is what I have so far:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection)
      For Each tn As TreeNode In col
      Console.WriteLine(drive_letter & treefix & tn.Text)
      If tn.Nodes.Count > 0 Then
      treefix = treefix & tn.Text & ""
      RecurseNodes(tn.Nodes)
      End If
      Next tn
      End Sub


      This is what my output looks like:






      E:Auftraggeber Projektname 00000
      E:Auftraggeber Projektname 00000Export
      E:Auftraggeber Projektname 00000Material
      E:Auftraggeber Projektname 00000MaterialOriginal
      E:Auftraggeber Projektname 00000MaterialTranscodiert
      E:Auftraggeber Projektname 00000MaterialFootage
      E:Auftraggeber Projektname 00000MaterialGFX
      E:Auftraggeber Projektname 00000MaterialSFX
      E:Auftraggeber Projektname 00000MaterialSprecher
      E:Auftraggeber Projektname 00000MaterialProjekt
      E:Auftraggeber Projektname 00000MaterialMusik





      and this is what the tree actually looks like:
      TreeView



      So once the code gets to E:Auftraggeber Projektname 00000MaterialFootage, it should go back the upper folder to continue processing.



      It is probably just a single line or two of code but I just can't figure it out.
      Thanks for your help!










      share|improve this question













      I want to build a function to recurse through a treeview and create each folder on a harddrive. The code is working fine so far. Once it reaches the end of a node it jumps one level deeper. But whta I am missing here is a line of code to jump back to the upper folder to continue creating the other folders.
      This is what I have so far:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection)
      For Each tn As TreeNode In col
      Console.WriteLine(drive_letter & treefix & tn.Text)
      If tn.Nodes.Count > 0 Then
      treefix = treefix & tn.Text & ""
      RecurseNodes(tn.Nodes)
      End If
      Next tn
      End Sub


      This is what my output looks like:






      E:Auftraggeber Projektname 00000
      E:Auftraggeber Projektname 00000Export
      E:Auftraggeber Projektname 00000Material
      E:Auftraggeber Projektname 00000MaterialOriginal
      E:Auftraggeber Projektname 00000MaterialTranscodiert
      E:Auftraggeber Projektname 00000MaterialFootage
      E:Auftraggeber Projektname 00000MaterialGFX
      E:Auftraggeber Projektname 00000MaterialSFX
      E:Auftraggeber Projektname 00000MaterialSprecher
      E:Auftraggeber Projektname 00000MaterialProjekt
      E:Auftraggeber Projektname 00000MaterialMusik





      and this is what the tree actually looks like:
      TreeView



      So once the code gets to E:Auftraggeber Projektname 00000MaterialFootage, it should go back the upper folder to continue processing.



      It is probably just a single line or two of code but I just can't figure it out.
      Thanks for your help!






      E:Auftraggeber Projektname 00000
      E:Auftraggeber Projektname 00000Export
      E:Auftraggeber Projektname 00000Material
      E:Auftraggeber Projektname 00000MaterialOriginal
      E:Auftraggeber Projektname 00000MaterialTranscodiert
      E:Auftraggeber Projektname 00000MaterialFootage
      E:Auftraggeber Projektname 00000MaterialGFX
      E:Auftraggeber Projektname 00000MaterialSFX
      E:Auftraggeber Projektname 00000MaterialSprecher
      E:Auftraggeber Projektname 00000MaterialProjekt
      E:Auftraggeber Projektname 00000MaterialMusik





      E:Auftraggeber Projektname 00000
      E:Auftraggeber Projektname 00000Export
      E:Auftraggeber Projektname 00000Material
      E:Auftraggeber Projektname 00000MaterialOriginal
      E:Auftraggeber Projektname 00000MaterialTranscodiert
      E:Auftraggeber Projektname 00000MaterialFootage
      E:Auftraggeber Projektname 00000MaterialGFX
      E:Auftraggeber Projektname 00000MaterialSFX
      E:Auftraggeber Projektname 00000MaterialSprecher
      E:Auftraggeber Projektname 00000MaterialProjekt
      E:Auftraggeber Projektname 00000MaterialMusik






      vb.net recursion treeview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 16:07









      max07

      398




      398
























          1 Answer
          1






          active

          oldest

          votes


















          2














          The simple solution in this case is to store the old value of the treefix variable before changing it, then revert to it after the inner RecurseNodes() call has exited:



          If tn.Nodes.Count > 0 Then
          Dim currentPath As String = treefix
          treefix = treefix & tn.Text & ""

          RecurseNodes(tn.Nodes)

          treefix = currentPath
          End If


          Though to avoid having to use class level variables, the more sustainable solution is to just pass the current path to your method. Also, do prefer Path.Combine() when working with paths in general:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection, ByVal currentDirectory As String)
          For Each tn As TreeNode In col
          Dim currentPath As String = Path.Combine(currentDirectory, tn.Text)
          Console.WriteLine(currentPath)

          If tn.Nodes.Count > 0 Then
          RecurseNodes(tn.Nodes, currentPath)
          End If
          Next tn
          End Sub


          Example usage:



          RecurseNodes(TreeView1.Nodes, "E:Auftraggeber Projektname 00000")





          share|improve this answer





















          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            Nov 12 '18 at 18:06










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            Nov 12 '18 at 18:08











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265946%2frecurse-through-treeview-and-create-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          The simple solution in this case is to store the old value of the treefix variable before changing it, then revert to it after the inner RecurseNodes() call has exited:



          If tn.Nodes.Count > 0 Then
          Dim currentPath As String = treefix
          treefix = treefix & tn.Text & ""

          RecurseNodes(tn.Nodes)

          treefix = currentPath
          End If


          Though to avoid having to use class level variables, the more sustainable solution is to just pass the current path to your method. Also, do prefer Path.Combine() when working with paths in general:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection, ByVal currentDirectory As String)
          For Each tn As TreeNode In col
          Dim currentPath As String = Path.Combine(currentDirectory, tn.Text)
          Console.WriteLine(currentPath)

          If tn.Nodes.Count > 0 Then
          RecurseNodes(tn.Nodes, currentPath)
          End If
          Next tn
          End Sub


          Example usage:



          RecurseNodes(TreeView1.Nodes, "E:Auftraggeber Projektname 00000")





          share|improve this answer





















          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            Nov 12 '18 at 18:06










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            Nov 12 '18 at 18:08
















          2














          The simple solution in this case is to store the old value of the treefix variable before changing it, then revert to it after the inner RecurseNodes() call has exited:



          If tn.Nodes.Count > 0 Then
          Dim currentPath As String = treefix
          treefix = treefix & tn.Text & ""

          RecurseNodes(tn.Nodes)

          treefix = currentPath
          End If


          Though to avoid having to use class level variables, the more sustainable solution is to just pass the current path to your method. Also, do prefer Path.Combine() when working with paths in general:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection, ByVal currentDirectory As String)
          For Each tn As TreeNode In col
          Dim currentPath As String = Path.Combine(currentDirectory, tn.Text)
          Console.WriteLine(currentPath)

          If tn.Nodes.Count > 0 Then
          RecurseNodes(tn.Nodes, currentPath)
          End If
          Next tn
          End Sub


          Example usage:



          RecurseNodes(TreeView1.Nodes, "E:Auftraggeber Projektname 00000")





          share|improve this answer





















          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            Nov 12 '18 at 18:06










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            Nov 12 '18 at 18:08














          2












          2








          2






          The simple solution in this case is to store the old value of the treefix variable before changing it, then revert to it after the inner RecurseNodes() call has exited:



          If tn.Nodes.Count > 0 Then
          Dim currentPath As String = treefix
          treefix = treefix & tn.Text & ""

          RecurseNodes(tn.Nodes)

          treefix = currentPath
          End If


          Though to avoid having to use class level variables, the more sustainable solution is to just pass the current path to your method. Also, do prefer Path.Combine() when working with paths in general:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection, ByVal currentDirectory As String)
          For Each tn As TreeNode In col
          Dim currentPath As String = Path.Combine(currentDirectory, tn.Text)
          Console.WriteLine(currentPath)

          If tn.Nodes.Count > 0 Then
          RecurseNodes(tn.Nodes, currentPath)
          End If
          Next tn
          End Sub


          Example usage:



          RecurseNodes(TreeView1.Nodes, "E:Auftraggeber Projektname 00000")





          share|improve this answer












          The simple solution in this case is to store the old value of the treefix variable before changing it, then revert to it after the inner RecurseNodes() call has exited:



          If tn.Nodes.Count > 0 Then
          Dim currentPath As String = treefix
          treefix = treefix & tn.Text & ""

          RecurseNodes(tn.Nodes)

          treefix = currentPath
          End If


          Though to avoid having to use class level variables, the more sustainable solution is to just pass the current path to your method. Also, do prefer Path.Combine() when working with paths in general:



          Private Sub RecurseNodes(ByVal col As TreeNodeCollection, ByVal currentDirectory As String)
          For Each tn As TreeNode In col
          Dim currentPath As String = Path.Combine(currentDirectory, tn.Text)
          Console.WriteLine(currentPath)

          If tn.Nodes.Count > 0 Then
          RecurseNodes(tn.Nodes, currentPath)
          End If
          Next tn
          End Sub


          Example usage:



          RecurseNodes(TreeView1.Nodes, "E:Auftraggeber Projektname 00000")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 18:00









          Visual Vincent

          15.2k51948




          15.2k51948












          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            Nov 12 '18 at 18:06










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            Nov 12 '18 at 18:08


















          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            Nov 12 '18 at 18:06










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            Nov 12 '18 at 18:08
















          This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
          – max07
          Nov 12 '18 at 18:06




          This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
          – max07
          Nov 12 '18 at 18:06












          @max07 : Glad I could help! Happy coding!
          – Visual Vincent
          Nov 12 '18 at 18:08




          @max07 : Glad I could help! Happy coding!
          – Visual Vincent
          Nov 12 '18 at 18:08


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265946%2frecurse-through-treeview-and-create-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values