Home All Groups Group Topic Archive Search About

DirectoryInfo.GetFiles -> from all subfolders

Author
6 Apr 2006 6:15 PM
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

Author
6 Apr 2006 7:09 PM
Mrozu
i forgot. VB.NET 2003

Mrozu
Author
6 Apr 2006 7:36 PM
Tank
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

Mrozu wrote:
Show quoteHide quote
> i forgot. VB.NET 2003
>
> Mrozu
>
Author
6 Apr 2006 8:02 PM
Mrozu
great but it displaies only folders'es names. but how rebuild it for
files'es names in those folders?

Thx Mrozu
Author
6 Apr 2006 8:10 PM
zacks
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.
Author
6 Apr 2006 8:25 PM
Tank
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

Mrozu wrote:
Show quoteHide quote
> great but it displaies only folders'es names. but how rebuild it for
> files'es names in those folders?
>
> Thx Mrozu
>