Results 1 to 17 of 17

Thread: how can i easily loop through all the files in a folder?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    how can i easily loop through all the files in a folder?

    ?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Like this ?
    VB Code:
    1. Private Sub GetFiles(ByVal folder As String)
    2.         Dim files() As String = IO.Directory.GetFiles(folder)
    3.         For Each File As String In files
    4.             MessageBox.Show(File)
    5.         Next
    6.     End Sub

    Use

    VB Code:
    1. GetFiles("c:\")

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    yeah, but i want it to include all files in all subdirectories...

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Then this must be changed to recursive method . I'll give it a shot !

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    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..

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  7. #7

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    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...

  8. #8

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    i think that was my first contribution to the board...

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    What problems did you encounter ? I tested it and it works .

  10. #10

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    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...

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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:
    1. Dim arraylist As New arraylist


    2- Change to Function and return Array of Objects .

    VB Code:
    1. Public Function getFiles(ByVal path As String) As Object()
    2.         Try
    3.  
    4.             Application.DoEvents()
    5.             Dim subdirectories() As String = IO.Directory.GetDirectories(path)
    6.  
    7.             For i As Integer = 0 To subdirectories.GetUpperBound(0) Step 1 + i
    8.                 'messagebox.show("subdirectory found: " & subdirectory)
    9.                 getFiles(subdirectories(i)) 'here is your recursion!
    10.             Next i
    11.             Dim files() As String = IO.Directory.GetFiles(path)
    12.             Dim file As String
    13.             For Each file In files
    14.                 arraylist.Add(file)
    15.             Next file
    16.  
    17.         Catch ex As Exception
    18.             MsgBox(ex.Message)
    19.         End Try
    20.         Return arraylist.ToArray
    21.     End Function 'get files


    3-Call it this way :
    VB Code:
    1. For i As Integer = 0 To getFiles("c:\").Length - 1
    2.             Me.ListBox1.Items.Add(arraylist(i).ToString)
    3.         Next

  13. #13

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    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.

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  15. #15

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

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

  16. #16
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  17. #17
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I mean something like this :

    VB Code:
    1. 'Class Level Variables
    2. Private arraylist As New arraylist
    3. Private tmpPath As String
    4.  
    5.  
    6. Public Function getFiles(ByVal path As String) As Object()
    7.         tmpPath = tmpPath
    8.  
    9.         Try
    10.             Application.DoEvents()
    11.             Dim subdirectories() As String = IO.Directory.GetDirectories(path)
    12.             For Each subdirectory In subdirectories
    13.                 getFiles(subdirectory)
    14.             Next subdirectory
    15.             Dim files() As String = IO.Directory.GetFiles(path)
    16.             Dim file As String
    17.             For Each file In files
    18.                 arraylist.Add(file)
    19.             Next file
    20.  
    21.         Catch ex As Exception
    22.             MsgBox(ex.Message)
    23.         End Try
    24.         Return arraylist.ToArray
    25.     End Function 'get files
    26.  
    27.  
    28.  
    29. Usage :
    30.  
    31. If Me.tmpPath = Me.TextBox1.Text Then
    32.             Me.arraylist.Clear()
    33.             Exit Sub
    34.         Else
    35.             Me.arraylist.Clear()
    36.             For i As Integer = 0 To getFiles(Me.TextBox1.Text).Length - 1
    37.                 Me.ListBox1.Items.Add(arraylist(i).ToString)
    38.             Next
    39.             MsgBox(ListBox1.Items.Count.ToString)
    40.         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
  •  



Click Here to Expand Forum to Full Width