DirectoryInfo.GetFiles -> from all subfolders

  • Thread starter Thread starter Mrozu
  • Start date Start date
M

Mrozu

Hi
I have folder

Music

with subfolder

Rock

with subfolder

Kult

folder tree looks like this:

Music
-Rock
-Kult

and in folders called Kult and Rock I have txt files.

Do you have code, with directoryinfo, which can show names of all
files?? from first subfolder called Rock and from second called Kult? I
found loops by which i can display files from first subfolder Rock but
files in Kult isn't shown.

Sorry for my english, but you know... still learning

Thx Mrozu
 
You have to use recursive loops, where you're calling the function, from
the function itself.

e.g:

Public Function FindFiles(ByVal Path As String) As Boolean
Dim Directories As New IO.DirectoryInfo(Path)
Dim Directory As IO.DirectoryInfo

For Each Directory In Directories.GetDirectories
ListBox1.Items.Add(Directory.Name)

If Directory.GetDirectories.Length > 0 Then
FindFiles(Directory.FullName)
End If
Next
End Function

Should work, but not tested :)

- Tank
 
great but it displaies only folders'es names. but how rebuild it for
files'es names in those folders?

Thx Mrozu
 
The recusive function will need to provide the filenames first with the
GetFiles method feeding a For Each loop to an instance of the FileInfo
class.
 
Easy, you could do this:

Public Function FindFiles(ByVal Path As String) As Boolean
Dim Directories As New IO.DirectoryInfo(Path)
Dim Directory As IO.DirectoryInfo
Dim File As IO.FileInfo

For Each Directory In Directories.GetDirectories
For Each File In Directory.GetFiles
ListBox1.Items.Add(File.Name)
Next

If Directory.GetDirectories.Length > 0 Then
FindFiles(Directory.FullName)
End If
Next
End Function

- Tank
 
Back
Top