![]() |
Click here to advertise with us
|
|
|||||||
| VB.NET General Discussion VB.NET general discussion area |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
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:
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 Last edited by cjard; 12-02-2008 at 10:43 AM. |
|
||||
|
Most recursive problems can actually be solved using loops which may be easier. In the case of directory searching you would use a Stack or a Queue collection.
Stack is LastIn-FirstOut like the stack of plates in a restaurant Queue is FirstIn-FirstOut like the queue of people getting their food in the restaurant You put one directory into the Queue to start with then start a loop that runs While Queue Length Is Greater Than Zero, scan the current queued directory for more directories and push them into the queu, then process the files Obviously the loop keeps running while more directories are pushed in. To work out the directory depth in this way you'd probably have to count the number of \ in the path If you used a stack, then the search would dig down before it went across, if you use a queue it goes across before it goes down. A simple recursive search is more like a stack, you'd have to get a bit more complicated with recursion to do a breadth-first search - If you can say what is confusing you about recursion I can possibly explain it |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|