Recursive Search problem

Posted 02 July 2009 - 02:18 PM

hi guys,

code as follows

Private Function RecursiveSearch1(ByVal path As String) As Boolean
On Error Resume Next
Dim lookfor As String = TextBox1.Text
Dim extensions As String() = lookfor.Split(New Char() {";"c})
Dim myfileinfos As New ArrayList()
Dim dirInfo As New IO.DirectoryInfo(path)
Dim driinfo As New IO.DriveInfo(path)
For Each ext As String In extensions
Dim aryFi As IO.FileInfo() = dirInfo.GetFiles(ext)
Dim fi As IO.FileInfo
For Each fi In aryFi
tempstring = fi.Name
AccessControl2Copy()
Next
Next
Dim aryFol As IO.DirectoryInfo() = dirInfo.GetDirectories
Dim di As IO.DirectoryInfo
For Each di In aryFol
RecursiveSearch1(di.FullName)
Next
Return True
End Function
Private Sub AccessControl2Copy()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf AccessControl2Copy))
Else
ListBox1.Items.Add(tempstring)
End If
End Sub

When I run with path being a folder, all is good. But if I run with path being a drive instead of a folder in a drive, once the search hits the last file, it keeps repeating "ListBox1.Items.Add(tempstring)". I can't figure out why... Any1 knows? Again, If I select a folder only, and there are thousands of subsubsubsubsub folders, its fine. But as soon as I select a drive, the problem occurs...

Replies To: Recursive Search problem

Re: Recursive Search problem

Posted 02 July 2009 - 02:39 PM

Btw. This is my first attempt at recursive. there're a few unused codes in there, so dnt worry about these....

also, maybe there's a better way to do it, but that I am unaware of; basically i want to be able to search and list all files within a directory/drive and sub-directories with particular extensions that can be changed.

Re: Recursive Search problem

Posted 02 July 2009 - 05:02 PM

My first suggestion would be to not use "On Error Resume Next" in .Net, this is retained for backwards compatibility but there is little if any reason to continue using it because vb.net features structured exception handling.
It works like:

For Each ext As String In extensions
Dim aryFi As IO.FileInfo() = dirInfo.GetFiles(ext)
Dim fi As IO.FileInfo
For Each fi In aryFi
tempstring = fi.Name
AccessControl2Copy()
Next
Next

I don't see why you have to re-iterate through the same directory to get files of each type. You should be able to simply run through it once and check each file extension for the ones that you are looking for. But really, all in all, yes there is a better way to do this.
I personally would use two methods, one to do the recursion through directories and another to just get the files contained within. Like in this little example:

Dim strExts() As String = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff"}
Public Sub GetDirs(ByVal path As String, Optional ByVal getSubDirs As Boolean = True)
Try
For Each subDir As String In Directory.GetDirectories(path)
GetFiles(subDir)
If getSubDirs Then
GetDirs(subDir)
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Sub
Public Sub GetFiles(ByVal path As String)
Try
For Each strFile As String In Directory.GetFiles(path)
Dim fInfo As New FileInfo(strFile)
'If fInfo.Extension = ".png" Then
If strExts.Contains(fInfo.Extension) Then
lstFiles.Items.Add(fInfo.FullName)
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Sub

Just have a listbox named "lstFiles" on the form and a button to start it like so:

Re: Recursive Search problem

Re: Recursive Search problem

Posted 05 July 2009 - 12:02 PM

Ok. I tried the code. Works orsm. Just that, when I select a drive. After It cathes an error, it basically fails. It doesn't search anymore.

Hence when a drive is selected, there is no permission to access "System Volume Information". And the code stops completely after it catches that error. How do I get around that other than by using "on error resume next", so that the program keeps retrieving other folders?