6 Replies - 1765 Views - Last Post: 07 July 2009 - 12:53 PM Rate Topic: -----

#1 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

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...

please help asap

cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive Search problem

#2 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

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.

cheers

This post has been edited by Ameel: 02 July 2009 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

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:
Try
		'Your code
		'Goes here
Catch ex As Exception
		Debug.WriteLine(ex.ToString)
End Try


Also, this code seems like it would be slow, and here especially:
		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:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		GetFiles("C:\Users") 'Get the files in this path
		GetDirs("C:\Users") 'Start recursion
	End Sub


See if you can learn from that and adapt it to fit your needs.
By the way, make sure to put this line at the very top of your source file so you don't have to keep typing "[System.]IO.-":
Imports System.IO	'This has to be there

Public Class Form1
	'-----------------------------------
End Class

This post has been edited by LoveIsNull: 02 July 2009 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Recursive Search problem

Posted 05 July 2009 - 11:21 AM

thanks for all that. will try them out now. ch33rs
Was This Post Helpful? 0
  • +
  • -

#5 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

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?

cheers
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Recursive Search problem

Posted 05 July 2009 - 02:04 PM

Suggest you examine the tutorial: Finding Files
Was This Post Helpful? 0
  • +
  • -

#7 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Recursive Search problem

Posted 07 July 2009 - 12:53 PM

thanks Adam. Good stuff
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1