Question Recursive searching

OsherLandes

Member
Joined
Oct 11, 2010
Messages
5
Location
England
Programming Experience
3-5
ok so basicly ive been attempting to make an application in wich the user puts a file path into a textbox and then clicks a button and the all the subdirectories adn the subdirectories of the subdirectories of the subdirectories etc... are added to a listbox below. It all seems to be working fine but i cant help thinking that the code may not actually be getting all the directories and subdirectories so im going to post my code in the hope that people can point out my errors to me:

Imports System.IO
Imports System.Windows.Forms.Application
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear() 'clear listbox

Dim index2 As Integer = 0 'declare index

'check for input
If TextBox1.Text = "" Then
MsgBox("No Directory Entered")
Else
'check if path exists
If System.IO.Directory.Exists(TextBox1.Text) = False Then
MsgBox("Directory Does Not Exist")
Else
'add original path to list of directories
ListBox1.Items.Add(TextBox1.Text)
'declare variables
Dim startdirs() As String = Directory.GetDirectories(TextBox1.Text)
Dim dirs(9999999) As String
Dim roots(9999999) As String
'load the first directories to check inte the variable dirs to begin loop
For i As Integer = 0 To startdirs.GetUpperBound(0)
dirs(i) = startdirs(i)
ListBox1.Items.Add(dirs(i))
Next
Do

DoEvents()

For i1 As Integer = 0 To dirs.GetUpperBound(0)
DoEvents()
If dirs(i1) = "" Then
Exit For
End If
Try
'get root directorys
Dim rootsofdir() As String = Directory.GetDirectories(dirs(i1))

For i As Integer = 0 To rootsofdir.GetUpperBound(0)
'add subdirectories of directory to list of subdirectories
roots(index2) = rootsofdir(i)
index2 += 1
If rootsofdir(i) = "" Then
index2 -= 1
End If
Next
'catch exception of access denied
Catch ex As UnauthorizedAccessException
MsgBox(ex.Message)
End Try
Next

index2 = 0 ' reset index

For i As Integer = 0 To roots.GetUpperBound(0)
DoEvents()
'add list of subdirectories to listbox
If roots(i) = "" Then
Exit For
End If
ListBox1.Items.Add(roots(i))
Next

For i As Integer = 0 To dirs.GetUpperBound(0)
DoEvents()
'make sure array dirs() is clear
If dirs(i) = "" Then
Exit For
End If
dirs(i) = ""
Next

For i As Integer = 0 To roots.GetUpperBound(0)
DoEvents()
'add contents of roots(subdirectoies) to dirs to be checked
If roots(i) = "" Then
Exit For
End If
dirs(i) = roots(i)
'cleard roots array as it goes along
roots(i) = ""
Next
index2 = 0 ' reset index2
Loop Until dirs(0) = "" 'end when no more directories are left
MsgBox("Finished")
End If
End If
End Sub
End Class
 
1st of all; use the code tag when posting code. Rules are rules.

2nd: The obvious error on this code is not to use built-in straight forward function easily available in the .net framework
VB.NET:
System.IO.Directory.GetDirectories(Pathstring, "*",SearchOption.AllDirectories)
and there you go, you got a list of all subdirectories within the PathString
 
Last edited:
thankyou for your response... yes i realise that the use of that line of code would be easier but im attempting to learn the language so im willing to go the long way round on things like this as i find that in doing so i learn the language better
 
Philosophically speaking I understand your point, but I do not agree with it.
Part of knowing a language is to dominate the little functions that will make your life easier while coding.
But, anyway, I admire your persistence, and from I read of your code, I'll give my 2 cents:

VB.NET:
Dim dirs(9999999) As String
Dim roots(9999999) As String
Those declarations are not the best way to deal with the problem you proposed. You still have the limitation of 9999999 and you're using way more memory than necessary.
In .net you have the class 'Collection' where you can add and remove items on the fly.
Something similar to:
VB.NET:
dim MyCollection as New Collection
.... then  ....
MyColletion.Items.Add(PathString)
.. then to access it ...
MyCollection(1).Value
I guess you got the idea; and then I'm sure you just realised that the whole: "roots.GetUpperBound(0)" will be gone and replaced by a much more accurate MyCollection.Items.Count
then the whole
VB.NET:
If roots(i) = "" Then
Exit For
will be gone too

furthermore, at the end of all, I'm sure there should be a direct way to just set MyCollection.Items to be your list box items.

happy coding...
 
"Recursive searching" as in your thread title, that is what you need to do, which is as far as I can see (which is barely in that post) not what you're doing. From recursive search:
How do you move a stack of 100 boxes? Answer: you move one box, remember where you put it, and then solve the smaller problem: how do you move a stack of 99 boxes? Eventually, you're left with the problem of how to move a single box, which you know how to do.
So how do you get subdirectories of a path? You already know that. Being left with writing a recursive method a common simplistic definition is 'a method that calls itself (with new arguments)'. Let's first define a method that lists subdirectories:
VB.NET:
Sub ListSubDirectories(path As String)
    For Each subdir in Directory.GetDirectories(path)
        Debug.WriteLine(subdir)
    Next
End Sub
When you want the list who do you call? ListSubDirectories("the path")
Can this method be reused? Yes, you can call it for any given path.
So how about the next "99 boxes", well, now you have a method you can call: ListSubDirectories(subdir)
Combining these and the goal is reached:
VB.NET:
Sub ListSubDirectories(path)
    For Each subdir in Directory.GetDirectories(path)
        Debug.WriteLine(subdir)
        ListSubDirectories(subdir)
    Next
End Sub
voila, a recursive directory search.

About Collection, Budius, (and ArrayList and HashTable for that matter), avoid these Object type based collections if you're not using .Net 1 (avoid that too), instead use the List(Of T) where T is the type of values putting into it, for example List(Of String). Collection class also has the disadvantage of 1-based indexes, whereas everything else in .Net is 0-based. It is really just a confusing VB6 leftover.
 
Back
Top