FileIterator class needs to dig a little deeper

RPY

New member
Joined
Jul 20, 2006
Messages
3
Programming Experience
Beginner
I'm trying to write a FileIterator class, and what I need to do is list all the files in any given pathway include all subdirectories, digging as far as the hierarchy goes. See my code below, and the problem is that it only searches down one level of the folder structure.

Any help appreiated :)


VB.NET:
Private Sub populate(ByVal includeSubDirs As Boolean)
 
Dim dirRoot As New DirectoryInfo(pathway)
Dim subDirs() As DirectoryInfo
Dim dir As DirectoryInfo
Dim fileList() As FileInfo
 
'root Dir files
fileList = dirRoot.GetFiles
addFilesInThisDir(fileList)
 
'include subDirs?
If Not includeSubDirs Then Exit Sub
 
'set subDirs
subDirs = dirRoot.GetDirectories '??? thisonly digs down 1 level :mad:
 
'sub Dir level 1
For Each dir In subDirs
 
If dirIsValid(dir) Then 'all this does is ignore sys/hidden folders
fileList = dir.GetFiles
addFilesInThisDir(fileList)
End If
Next dir
 
End Sub
 
Last edited by a moderator:
You need to use recursion to 'keep the loop digging' Here's a snippet i got from the code project site. It was in c# but i did a quick conversion. See if it helps..

VB.NET:
'Create a Directory object using DirectoryInfo 
   Dim dir As DirectoryInfo = New DirectoryInfo('Path')
   'Pass the Directory for displaying the contents
   getDirsFiles(dir)
  'this is the recursive function
  Public Shared Sub getDirsFiles(ByVal d As DirectoryInfo)
   'create an array of files using FileInfo object
   Dim files As FileInfo()
   'get all files for the current directory
   files = d.GetFiles("*.*")
   'iterate through the directory and print the files
   For Each file As FileInfo In files
    'get details of each file using file object
    Dim fileName As String = file.FullName
    Dim fileSize As String = file.Length.ToString()
    Dim fileExtension As String =file.Extension
    Dim fileCreated As String = file.LastWriteTime.ToString()
    io.WriteLine(fileName & " " & fileSize & " " & fileExtension & " " & fileCreated)
   Next file
   'get sub-folders for the current directory
   Dim dirs As DirectoryInfo() = d.GetDirectories("*.*")
   'This is the code that calls 
   'the getDirsFiles (calls itself recursively)
   'This is also the stopping point 
   '(End Condition) for this recursion function 
   'as it loops through until 
   'reaches the child folder and then stops.
   For Each dir As DirectoryInfo In dirs
    io.WriteLine("--------->> {0} ", dir.Name)
    getDirsFiles(dir)
   Next dir
  End Sub
 
Brilliant - that has done the trick :)
thanks very much for the help

here's my solution.......

VB.NET:
Private Sub populate(ByVal includeSubDirs As Boolean)
 
'create a directory
Dim dir As DirectoryInfo = New DirectoryInfo(pathway)
 
'pass the directory
addFilesInThisDir(dir)
 
'call recuresive function for the sub directories
If includeSubDirs Then doSubDirs(dir)
 
End Sub
 
Private Sub doSubDirs(ByVal d As DirectoryInfo)
 
'add files in this directory
addFilesInThisDir(d)
 
'get subdirectories
Dim subDirs As DirectoryInfo() = d.GetDirectories
 
For Each dir As DirectoryInfo In subDirs
doSubDirs(dir)
Next dir
 
End Sub
 
Last edited by a moderator:
VB 2005 already has the My.Computer.FileSystem.GetDirectories and My.Computer.FileSystem.GetFiles methods to do this. The Framework also has the IO.Directory.GetDirectories and IO.Directory.GetFiles methods. The one advantage of doing it yourself is that if you encounter a folder that you do not have permission to access you can just ignore it, while those other methods will throw an exception.
 
Bypass a folder

The code works great and you mentioned bypassing a folder if you don't have permissions to it. How do you do that?
 
Back
Top