[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
Re: Getting all files on a drive regardless of folders?
You'll need to do a recursive method for each directory found.
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:
Public Sub TouchAllFiles(ByVal folderPath As String)
For Each filePath As String In IO.Directory.GetFiles(folderPath)
'Use filePath here.
Next
For Each subfolderPath As String In IO.Directory.GetDirectories(folderPath)
Me.TouchAllFiles(subfolderPath)
Next
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.
Re: Getting all files on a drive regardless of folders?
Thanks, JMC. That's what I needed!