Results 1 to 4 of 4

Thread: [RESOLVED] Getting all files on a drive regardless of folders?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Resolved [RESOLVED] Getting all files on a drive regardless of folders?

    I'm using the following code to get all of the files from a DVD drive and load them into a bindinglist and then a datagridview:

    Code:
             mAddBL.Clear()
             '
            Try
                Dim dir As New IO.DirectoryInfo(Form1.m_DVDdrive & ":\")
                Dim filesarray As IO.FileInfo() = dir.GetFiles()
                Dim file As IO.FileInfo
                Dim filename As String
                Dim extpos As Integer
                '
                For Each file In filesarray
                    '
                    extpos = InStr(file.Name, ".")
                    If extpos > 0 Then
                        filename = Microsoft.VisualBasic.Left(file.Name, extpos - 1)
                    End If
                    '
                    mAddBL.Add(New qckMovies(filename, "DVD", "Movie", True))
                    '
                Next
                '
                SetdgvAdd()
                '
            Catch ex As Exception
                MessageBox.Show(ex.Message, "No DVD or CD in Drive")
                Me.Close()
            End Try
    This shows all files and folder names on the disc. If I want it to show all files, regardless of the folder they're in, is there a built-in method to do that or do I need to take an entry, identify it as a directory and then use that as my source to read the files in it? thanks

  2. #2

    Re: Getting all files on a drive regardless of folders?

    You'll need to do a recursive method for each directory found.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting all files on a drive regardless of folders?

    As formlesstree4 says, you'll need to use recursion. A recursive method is one that calls itself. In this case, you will call a method and specify a folder and that method will then call itself for each subfolder. In it's most basic form a recursive file search will look like this:
    vb.net Code:
    1. Public Sub TouchAllFiles(ByVal folderPath As String)
    2.     For Each filePath As String In IO.Directory.GetFiles(folderPath)
    3.         'Use filePath here.
    4.     Next
    5.  
    6.     For Each subfolderPath As String In IO.Directory.GetDirectories(folderPath)
    7.         Me.TouchAllFiles(subfolderPath)
    8.     Next
    9. End Sub
    You can obviously adapt and extend that as required for your particular scenario. You should also note that you'll need to include an exception handler somewhere so that you can continue the search if you hit an inaccessible folder, which is something that the inbuilt recursion of GetFiles doesn't provide for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Getting all files on a drive regardless of folders?

    Thanks, JMC. That's what I needed!
    Last edited by dkahn; Sep 27th, 2009 at 09:45 PM.

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