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.






MultiQuote





|