Recurse through TreeView and create folders











up vote
0
down vote

favorite












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


























    up vote
    0
    down vote

    favorite












    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
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 20 hours ago









      max07

      398




      398
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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
            18 hours ago










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            18 hours ago











          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',
          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
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          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
            18 hours ago










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            18 hours ago















          up vote
          2
          down vote



          accepted










          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
            18 hours ago










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            18 hours ago













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          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 18 hours ago









          Visual Vincent

          14.9k51947




          14.9k51947












          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            18 hours ago










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            18 hours ago


















          • This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
            – max07
            18 hours ago










          • @max07 : Glad I could help! Happy coding!
            – Visual Vincent
            18 hours ago
















          This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
          – max07
          18 hours ago




          This is exactly what I needed. Passing the current path was a simple, but great idea. Thanks!
          – max07
          18 hours ago












          @max07 : Glad I could help! Happy coding!
          – Visual Vincent
          18 hours ago




          @max07 : Glad I could help! Happy coding!
          – Visual Vincent
          18 hours ago


















           

          draft saved


          draft discarded



















































           


          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




















































































          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?