I dont understand why you have a procedure for calculating a the subdirectory level. Surely it's just +1 of whatever youre in now, so when youre processing a directory, you should call:
If (current thing is a directory) Then
ProcessDirectory(thing, currentLevel + 1)
Your level starts at 0, and searches the dir.. So every folder that is found during the search will be called with 0+1
This 0+1 (=1) becomes the subDirLevel for the recursed call
Then every folder found during that search would be (currentSubDirLevel of 1)+1 =2
And so on
Some other tips to unclutter your code:
String has a constructor that takes a character, and the number of times to repeat it. This would be preferable to using a & to concatenate '-' on hundreds or thousands of times:
Code:
' Spacing is used for indentation
Dim sSpacing As String = New String("-"c, mySubDirLevel)
If mySubDirLevel > 0 Then
For nIdx As Integer = 0 To mySubDirLevel
sSpacing = sSpacing & "-"
Next nIdx
End If
Code:
If subdirectoryEntries.Length > 0 Then
For Each subdirectory As String In subdirectoryEntries
' RECURSE - Process the Directory
ProcessDirectory(subdirectory, mySubDirLevel + 1)
Next
End If
End Sub
Quote:
' Global used to keep track of previous Dir Level
Dim nDirLevelCounter As Integer = 1
|
'global? No no, do not use anything global when recursing. The whole idea of recursion is that you pass everything you want to use when youre using it, that way the recursion can start and finish in any logical state. This is probably why your other code was faulty, because your global variable is being set incorrectly.
Hopefully you can see how calculating the level relative to the base is pointless compared to simply declaring that base = 0 and every tiem you recurse, just passing in currentLevel + 1