?
Printable View
?
Like this ?
VB Code:
Private Sub GetFiles(ByVal folder As String) Dim files() As String = IO.Directory.GetFiles(folder) For Each File As String In files MessageBox.Show(File) Next End Sub
Use
VB Code:
GetFiles("c:\")
yeah, but i want it to include all files in all subdirectories...
Then this must be changed to recursive method . I'll give it a shot !
if that is the best way, i'll do it and post it here... i thought there might be a more efficient function already made though..
No , there isn't any method that traverse all folders and subfolders . Even if you used APIs FindFile and FindNext , you still have to use recursive mode .
here it is:
i tested and it works.Code:Public Sub getFiles(ByVal path as string)
dim subdirectories() as string = io.directory.getdirectories(path)
dim subdirectory as string
for each subdirectory in subdirectories
messagebox.show("subdirectory found: " & subdirectory)
getfiles(subdirectory) 'here is your recursion!
next subdirectory
dim files() as string = io.directory.getfiles(path)
dim file as string
for each file in files
messagebox.show("file found: " & file)
next file
end sub 'get files
can someone update it so it returns a string array of all files? i run into problems when i try...
i think that was my first contribution to the board...
What problems did you encounter ? I tested it and it works .
the code i posted works.
but can you update it so that it returns a string array of all files?
i don't know where to define the array so that not a new one gets created every time the recursion occurs, and i don't want to define it outside of the function...
Ok , your code needs a final touch , use Application.DoEvents() in the first line of that method . This will boost it and for returning array of files names , try to add it to a listbox instead of showing them in messagebox .
;)
Do these steps to return ArrayList that contains all files found . Note this is not the only way .
1-Declare a class level ArrayList object like this :
VB Code:
Dim arraylist As New arraylist
2- Change to Function and return Array of Objects .
VB Code:
Public Function getFiles(ByVal path As String) As Object() Try Application.DoEvents() Dim subdirectories() As String = IO.Directory.GetDirectories(path) For i As Integer = 0 To subdirectories.GetUpperBound(0) Step 1 + i 'messagebox.show("subdirectory found: " & subdirectory) getFiles(subdirectories(i)) 'here is your recursion! Next i Dim files() As String = IO.Directory.GetFiles(path) Dim file As String For Each file In files arraylist.Add(file) Next file Catch ex As Exception MsgBox(ex.Message) End Try Return arraylist.ToArray End Function 'get files
3-Call it this way :
VB Code:
For i As Integer = 0 To getFiles("c:\").Length - 1 Me.ListBox1.Items.Add(arraylist(i).ToString) Next
yeah, using doevents is a good idea...
MSDN:
When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.
If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.
Did you get it working now with arrays ? It returns an array . You use the name of the function as if it's an array variable .
Public Function getFiles(ByVal path As String) As Object()
Try
Application.DoEvents()
Dim subdirectories() As String = IO.Directory.GetDirectories(path)
For Each subdirectory In subdirectories
getfiles(subdirectory)
Next subdirectory
Dim files() As String = IO.Directory.GetFiles(path)
Dim file As String
For Each file In files
arraylist.Add(file)
Next file
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return arraylist.ToArray
End Function 'get files
that is how i did it now...
but if i call the function twice, it wont work unless i reset arraylist, which is class level, because its not on the level of the function. not perfect yet...
Then declare a variable that takes the path in the function as class level also , before calling the function again , check if it contains the same string then don't do anything or else do reset the arraylist and do new search .
I mean something like this :
VB Code:
'Class Level Variables Private arraylist As New arraylist Private tmpPath As String Public Function getFiles(ByVal path As String) As Object() tmpPath = tmpPath Try Application.DoEvents() Dim subdirectories() As String = IO.Directory.GetDirectories(path) For Each subdirectory In subdirectories getFiles(subdirectory) Next subdirectory Dim files() As String = IO.Directory.GetFiles(path) Dim file As String For Each file In files arraylist.Add(file) Next file Catch ex As Exception MsgBox(ex.Message) End Try Return arraylist.ToArray End Function 'get files Usage : If Me.tmpPath = Me.TextBox1.Text Then Me.arraylist.Clear() Exit Sub Else Me.arraylist.Clear() For i As Integer = 0 To getFiles(Me.TextBox1.Text).Length - 1 Me.ListBox1.Items.Add(arraylist(i).ToString) Next MsgBox(ListBox1.Items.Count.ToString) End If