|
-
Jan 4th, 2004, 08:45 AM
#1
Thread Starter
Registered User
how can i easily loop through all the files in a folder?
-
Jan 4th, 2004, 08:53 AM
#2
Sleep mode
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
-
Jan 4th, 2004, 08:57 AM
#3
Thread Starter
Registered User
yeah, but i want it to include all files in all subdirectories...
-
Jan 4th, 2004, 09:01 AM
#4
Sleep mode
Then this must be changed to recursive method . I'll give it a shot !
-
Jan 4th, 2004, 09:06 AM
#5
Thread Starter
Registered User
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..
-
Jan 4th, 2004, 09:12 AM
#6
Sleep mode
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 .
-
Jan 4th, 2004, 09:32 AM
#7
Thread Starter
Registered User
here it is:
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
i tested and it works.
can someone update it so it returns a string array of all files? i run into problems when i try...
-
Jan 4th, 2004, 09:35 AM
#8
Thread Starter
Registered User
i think that was my first contribution to the board...
-
Jan 4th, 2004, 10:15 AM
#9
Sleep mode
What problems did you encounter ? I tested it and it works .
-
Jan 4th, 2004, 10:19 AM
#10
Thread Starter
Registered User
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...
-
Jan 4th, 2004, 10:19 AM
#11
Sleep mode
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 .
-
Jan 4th, 2004, 10:33 AM
#12
Sleep mode
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
-
Jan 4th, 2004, 10:35 AM
#13
Thread Starter
Registered User
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.
Last edited by marvinklein; Jan 4th, 2004 at 10:40 AM.
-
Jan 4th, 2004, 10:59 AM
#14
Sleep mode
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 .
-
Jan 4th, 2004, 11:07 AM
#15
Thread Starter
Registered User
working...
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...
-
Jan 4th, 2004, 11:12 AM
#16
Sleep mode
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 .
-
Jan 4th, 2004, 11:35 AM
#17
Sleep mode
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
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
|