Page 1 of 1

Finding Files Recursive & Non-Recursive Methods Rate Topic: ***** 1 Votes

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Post icon  Posted 09 May 2009 - 01:00 PM

Finding Files

Task: Find all the files of a particular file type (Mp3 in this case) on a drive.

It is possible to get the via this helpful method.
  My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories, "*.mp3")


but this method has a problem.
It isn't interactive, by that i mean it doesn't give you an update to where it is, how many has it found so for.

Both of the following subroutine use the following routine, so that they can be place inside a background worker.
 ' The delegate
 Delegate Sub SetLabelText_Delegate(ByVal [Label] As Label, ByVal [text] As String)

 ' The delegates subroutine.
 Private Sub SetLabelText_ThreadSafe(ByVal [Label] As Label, ByVal [text] As String)
  ' InvokeRequired required compares the thread ID of the calling thread to the thread ID of the creating thread.
  ' If these threads are different, it returns true.
  If [Label].InvokeRequired Then
   Dim MyDelegate As New SetLabelText_Delegate(AddressOf SetLabelText_ThreadSafe)
   Me.Invoke(MyDelegate, New Object() {[Label], [text]})
  Else
   [Label].Text = [text]
  End If
 End Sub



1. The Recursive Method

This is the probably the most common method used the achieve this task.
 Public Sub FindMyFiles_Recursive(ByRef thelist As List(Of String), ByVal currentdir As String, ByRef lbl As Label, ByRef lbl2 As Label)
  If lbl2 IsNot Nothing Then
   SetLabelText_ThreadSafe(lbl2, String.Format(":- {0}", currentdir))
  End If
  Try
   For Each filefound As String In My.Computer.FileSystem.GetFiles(currentdir, FileIO.SearchOption.SearchTopLevelOnly, "*.mp3")
	thelist.Add(filefound)
	If lbl IsNot Nothing Then
	 SetLabelText_ThreadSafe(lbl, String.Format("{0} Mp3s Found", thelist.Count))
	End If
   Next
   For Each dirfound As String In My.Computer.FileSystem.GetDirectories(currentdir, FileIO.SearchOption.SearchTopLevelOnly)
	FindMyFiles_Recursive(thelist, dirfound, lbl, lbl2)
   Next
  Catch ex As UnauthorizedAccessException
   Exit Sub
  End Try
 End Sub



Usage:
Dim Mp3s As New List(Of String)
FindMyFiles_Recursive(Mp3s, "c:\", Me.Label1, Me.Label2)



Recursive routines use the stack a lot and their is the possibility of it overflowing and crashing the program.

So a devised a non-recursive way of doing it.
2. Non-Recursive Method
 Public Sub findmyfiles_NonRecursive(ByRef thelist As List(Of String), ByVal currentdir As String, ByRef lbl As Label, ByRef lbl2 As Label)
  Dim q As New Queue(Of String)
  q.Enqueue(currentdir)
  While q.Count > 0
   currentdir = q.Peek
   If lbl2 IsNot Nothing Then
	SetLabelText_ThreadSafe(lbl2, String.Format(":- {0}", currentdir))
   End If
   q.Dequeue()
   Try
	For Each filefound As String In My.Computer.FileSystem.GetFiles(currentdir, FileIO.SearchOption.SearchTopLevelOnly, "*.mp3")
	 thelist.Add(filefound)
	 If lbl IsNot Nothing Then
	  SetLabelText_ThreadSafe(lbl, String.Format("{0} Mp3s Found", thelist.Count))
	 End If
	Next
	For Each dirfound As String In My.Computer.FileSystem.GetDirectories(currentdir, FileIO.SearchOption.SearchTopLevelOnly)
	 q.Enqueue(dirfound)
	Next
   Catch ex As UnauthorizedAccessException
	q.Dequeue()
	Continue While
   End Try
  End While
 End Sub


Usage:
Dim Mp3s As New List(Of String)
FindMyFiles_NonRecursive(Mp3s, "c:\", Me.Label1, Me.Label2)



So now you can interactively find files both recursively and non-recursively.

Is This A Good Question/Topic? 1
  • +

Replies To: Finding Files

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4313
  • View blog
  • Posts: 12,090
  • Joined: 18-April 07

Posted 10 May 2009 - 10:34 AM

I hardly call this a tutorial, perhaps more a snippet of some sort. Tutorials are suppose to actually teach things and explain the process rather than just throwing out the code.

Not too be too harsh, but I am surprised this type of stuff if making it past the admins/moderators in the way of tutorial quality.

You might want to invest a little more time in the process of actually writing out what is going on and explain to the reader what is going on. Some of them are reading a tutorial because they don't understand all the stuff you have going on here.

Just a tip for the future. :^: :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1