title: Loop through treeview nodes to delete certain nodes
body: <p>Hello and thanks in advance for your help.</p>
  
  <p>I have a treeview that is populated from a HDD folder collection. Its structure is similar to the following (however the structure is dynamic):</p>
  
  <pre><code>My Disk:
  |
  |--folder1(tag:folder)
  |  |--subfolder1(tag:folder)
  |--folder2(tag:folder)
  |--folder3(tag:folder)
  |--folder4(tag:folder)
     |file1(tag:file)
  </code></pre>
  
  <p>I would like to delete all nodes from the tree that do not contain nodes with the tag "file" (basically empty folders). I would like the resulting tree structure to look like this:</p>
  
  <pre><code>My Disk:
  |--folder4(tag:folder)
     |file1(tag:file)
  </code></pre>
  
  <p>The best that I have come up with is the following:</p>
  
  <pre><code>Private Sub deleteNode(byval nc as TreeNodeCollection)
   For Each tn As TreeNode In nc
    'delete node if it applies
          If tn.Tag = "folder" Then
              If tn.Nodes.Count = 0 Then
                   nc.Remove(tn)
              End If
          End If
  
          If tn.Nodes.Count &gt; 0 Then
               deleteNode(tn.Nodes)
          End If
      Next
  End Sub
  </code></pre>
  
  <p>I call the sub as follows:</p>
  
  <pre><code>deleteNode(treeview1.Nodes(0).Nodes)
  </code></pre>
  
  <p>However, the above is not working properly. It deletes only some nodes and not all the targeted nodes. Could you please help me out by fixing my sub?</p>
  
  <p>Many thanks</p>