|
-
Oct 8th, 2010, 08:19 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Not sure how to use this.
I found a file recursive directory function, but i'm not sure how to start it 
Any ideas?
Code:
Imports System.IO
''' <summary>
''' This class contains directory helper method(s).
''' </summary>
Public Class FileHelper
''' <summary>
''' This method starts at the specified directory, and traverses all subdirectories.
''' It returns a List of those directories.
''' </summary>
Public Shared Function GetFilesRecursive(ByVal initial As String) As List(Of String)
' This list stores the results.
Dim result As New List(Of String)
' This stack stores the directories to process.
Dim stack As New Stack(Of String)
' Add the initial directory
stack.Push(initial)
' Continue processing for each stacked directory
Do While (stack.Count > 0)
' Get top directory string
Dim dir As String = stack.Pop
Try
' Add all immediate file paths
result.AddRange(Directory.GetFiles(dir, "*.*"))
' Loop through all subdirectories and add them to the stack.
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
stack.Push(directoryName)
Next
Catch ex As Exception
End Try
Loop
' Return the list
Return result
End Function
End Class
Module Module1
''' <summary>
''' Entry point that shows usage of recursive directory function.
''' </summary>
Sub Main()
' Get recursive List of all files starting in this directory.
Dim list As List(Of String) = FileHelper.GetFilesRecursive("C:\Users\Sam\Documents\Perls")
' Loop through and display each path.
For Each path In list
Console.WriteLine(path)
Next
' Write total number of paths found.
Console.WriteLine(list.Count)
End Sub
End Module
-
Oct 8th, 2010, 08:52 PM
#2
Re: Not sure how to use this.
Dim ListOfFiles = FileHelper.GetFilesRecursive(<path of directory to start searching>)
That should work.
My usual boring signature: Nothing
 
-
Oct 8th, 2010, 09:10 PM
#3
Lively Member
Re: Not sure how to use this.
You can use the static GetFiles method of the Directory class & specify the last parameter as "SearchOption.AllDirectories". This will search recursively.
Code:
Dim myDirectories As String() = Directory.GetFiles("C:\", "*.*", SearchOption.AllDirectories)
-
Oct 8th, 2010, 10:25 PM
#4
Re: Not sure how to use this.
 Originally Posted by Mickroy
You can use the static GetFiles method of the Directory class & specify the last parameter as "SearchOption.AllDirectories". This will search recursively.
Code:
Dim myDirectories As String() = Directory.GetFiles("C:\", "*.*", SearchOption.AllDirectories)
That code will throw an exception when it inevitably encounters an inaccessible folder. If there's any chance of finding an inaccessible folder along the way then you must do the recursion yourself, so that you can handle the exception and continue.
-
Oct 8th, 2010, 10:44 PM
#5
Thread Starter
Frenzied Member
Re: Not sure how to use this.
Thank you for the reply.
Looks like the:
Dim ListOfFiles = FileHelper.GetFilesRecursive("C:\")
starts the "engine" and freezes the frm.
Which i was thinking of adding it to a background worker to start that process on a different thread so that the user still could interact with the main GUI.
But, one problem even if i change C:\ to C:\test which only has one file in my case it still does not display found files on the listbox1.
The code:
Code:
Imports System.IO
''' <summary>
''' This class contains directory helper method(s).
''' </summary>
Public Class FileHelper
''' <summary>
''' This method starts at the specified directory, and traverses all subdirectories.
''' It returns a List of those directories.
''' </summary>
Public Shared Function GetFilesRecursive(ByVal initial As String) As List(Of String)
' This list stores the results.
Dim result As New List(Of String)
' This stack stores the directories to process.
Dim stack As New Stack(Of String)
' Add the initial directory
stack.Push(initial)
' Continue processing for each stacked directory
Do While (stack.Count > 0)
' Get top directory string
Dim dir As String = stack.Pop
Try
' Add all immediate file paths
result.AddRange(Directory.GetFiles(dir, "*.*"))
' Loop through all subdirectories and add them to the stack.
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
stack.Push(directoryName)
Next
Catch ex As Exception
End Try
Loop
' Return the list
Return result
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ListOfFiles = FileHelper.GetFilesRecursive("C:\")
End Sub
End Class
Module Module1
Sub Main()
' Get recursive List of all files starting in this directory.
Dim list As List(Of String) = FileHelper.GetFilesRecursive("C:\Windows")
' Loop through and display each path.
For Each path In list
FileHelper.ListBox1.Items.Add(path)
MsgBox(path)
Next
' Write total number of paths found.
' FileHelper.ListBox1.Items.Add(list.Count)
End Sub
End Module
Can you see the problem?
Thanks in advance.
-
Oct 8th, 2010, 10:57 PM
#6
Re: Not sure how to use this.
Your code doesn't really make a lot of sense. You have a Main method in a module, which suggests that that would be the entry point for the app, yet you have at least one form but that Main method isn't creating a form, so the Main method obviously isn't the entry point. Besides that, you have a module calling a Shared method of a form to get a list of files and then displaying them in an instance of the same form class. Why would the form not just do that itself? What possible useful purpose could that module serve?
I'll wager that the issue is that you are adding the items to the ListBox on the default instance of the form but it's not the default instance that you have displayed but it's hard to tell because your code is just a bit non-sensical. From a functionality perspective, what are you actually trying to achieve? For example:
The user clicks a Button on a form and then selects a folder in a FolderBrowserDialog, then all files in and under that folder get displayed in a ListBox on the same form, without freezing the form in the process.
That's the sort of succinct, i.e. brief but clear, description I'm looking for.
-
Oct 8th, 2010, 11:14 PM
#7
Thread Starter
Frenzied Member
Re: Not sure how to use this.
The user clicks a Button on a form and then selects a folder in a FolderBrowserDialog, then all files in and under that folder get displayed in a ListBox on the same form, without freezing the form in the process.
That itself is not hard to make.
The problem is that when access to file is denied then it errors out.
So that's why I'm looking into GetFilesRecursive.
I mean frm does freeze up so that means it's searching for something right.
I have been always so confused with GetFilesRecursive.
-
Oct 9th, 2010, 12:06 AM
#8
Re: Not sure how to use this.
I specifically asked for a description from you of what you're trying to achieve.
From a functionality perspective, what are you actually trying to achieve?
You provided no such description. I assume that those who don't answer my questions don;t want my help.
-
Oct 9th, 2010, 07:57 AM
#9
Thread Starter
Frenzied Member
Re: Not sure how to use this.
Thank you for the reply jmcilhinney.
What I'm trying to do is to get all the files from specific directory.
I was going to use the Getfiles method.
The problem with that is when the code comes to a file that access is denied to its going to error out.
So that's why I'm looking into the GetFilesRecursive to skip such error when it invokes.
I just can't find a good example or article that would teach me. 
Any ideas?
Thank you in advance.
-
Oct 9th, 2010, 08:27 AM
#10
Re: Not sure how to use this.
That still doesn't answer my question so I'll just ignore that and address the recursion issue specifically. It's really very simple. The whole point of recursion is that you have a method that does something, then calls itself to do that thing again at a different level. In this case, you want a method that takes a folder path and gets all the files in that folder, then calls itself for each subfolder. Exactly how you implement it depends on what you want it to return. File.GetFiles retirns a String array so let's go with that.
Step 1: Write a method that gets all the files in a folder and returns them as a String array:
vb.net Code:
Private Function GetFiles(ByVal folder As String) As String() Dim files As String() = Directory.GetFiles(folder) Return files End Function
Step 2: Make the function recursive by having it call itself for each subfolder:
vb.net Code:
Private Function GetFiles(ByVal folder As String) As String() Dim files As New List(Of String)(Directory.GetFiles(folder)) For Each subfolder As String In Directory.GetDirectories(folder) 'This is the recursive call. files.AddRange(GetFiles(subfolder)) Next Return files.ToArray() End Function
Notice that we now start with a List, so that we can add to it, but we still return an array.
Step 3: Add exception handling to enable recursion to continue if an inaccessible folder is encountered:
vb.net Code:
Private Function GetFiles(ByVal folder As String) As String() Dim files As New List(Of String) Try files.AddRange(Directory.GetFiles(folder)) For Each subfolder As String In Directory.GetDirectories(folder) 'This is the recursive call. files.AddRange(GetFiles(subfolder)) Next Catch 'This folder is inaccessible. Ignore and continue. End Try Return files.ToArray() End Function
That's pretty much all there is to it.
-
Oct 9th, 2010, 08:36 AM
#11
Thread Starter
Frenzied Member
Re: Not sure how to use this.
Thank you for the code and the expiation jmcilhinney.
I have been very confused with recursion function.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|