School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,988 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,958 people online right now. Registration is fast and FREE... Join Now!




Finding Files

 
Reply to this topicStart new topic

> Finding Files, Recursive & Non-Recursive Methods

AdamSpeight2008
Group Icon



post 9 May, 2009 - 12:00 PM
Post #1


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

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

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

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:
CODE

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
CODE

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:
CODE

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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Martyr2
Group Icon



post 10 May, 2009 - 09:34 AM
Post #2
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. icon_up.gif smile.gif
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:42AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month