Link to home
Start Free TrialLog in
Avatar of scottealam
scottealamFlag for New Zealand

asked on

Recursive list of files in folder and all subfolders

Again - a newbie to ASP.NET I got some help with this question last week but another question has cropped up as a result.
I'm trying to get a list of all the files in a folder and all the files in all subfolders beneith it. I thought the code below would do the trick, but it doesn't seem to work quite right. It is listing the files in all the subfolders, but not the files within the top level folder and doesn't seem to look any deeper than one subfolder. Here is the code

Private Sub OnNodeSelect(ByVal virtualFolderPath As String)
     
        Dim Directories As New DirectoryInfo(virtualFolderPath)
        Dim Directory As DirectoryInfo
        Dim File As FileInfo
             
        Dim dt As New DataTable
       
        dt.Columns.Add("Name")
        dt.Columns.Add("FullName")
        dt.Columns.Add("LastWriteTime")
      
        Dim dr As DataRow      

        For Each Directory In Directories.GetDirectories
             For Each File In Directory.GetFiles
                dr = dt.NewRow
                dr("Name") = File.Name
                dr("FullName") = File.FullName
                dr("LastWriteTime") = File.LastWriteTime
                dt.Rows.Add(dr)
             Next

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

         FileList.DataSource = dt
         FileList.Databind()      
 
         End Sub
Avatar of RossWilko
RossWilko

Hi,

Seperating it out like this will do the trick for you...

             For Each File In Directory.GetFiles
                dr = dt.NewRow
                dr("Name") = File.Name
                dr("FullName") = File.FullName
                dr("LastWriteTime") = File.LastWriteTime
                dt.Rows.Add(dr)
             Next

        For Each Directory In Directories.GetDirectories
            If Directory.GetDirectories.Length > 0 Then
                 OnNodeSelect(Directory.FullName)
             End If
         Next

cheers
RW
Avatar of scottealam

ASKER

Thanks for your help Ross, but I am now getting a "System.NullReferenceException: Object reference not set to an instance of an object" error. I'm not sure why this would be, I have declared all my variables etc

Private Sub OnNodeSelect(ByVal virtualFolderPath As String)
     
     Dim Directories As DirectoryInfo
     Directories = New DirectoryInfo(virtualFolderPath)
     Dim Directory As DirectoryInfo
     Dim File As FileInfo
             
     Dim dt As New DataTable
      dt.Columns.Add("Name")
      dt.Columns.Add("FullName")
      dt.Columns.Add("LastWriteTime")

     Dim dr As DataRow      
             For Each File In Directory.GetFiles
                dr = dt.NewRow
                dr("Name") = File.Name
                dr("FullName") = File.FullName
                dr("LastWriteTime") = File.LastWriteTime
                dt.Rows.Add(dr)
             Next

             For Each Directory In Directories.GetDirectories
                 If Directory.GetDirectories.Length > 0 Then
                    OnNodeSelect(Directory.FullName)
                 End If
             Next

             FileList.DataSource = dt
             FileList.Databind()

End Sub
ASKER CERTIFIED SOLUTION
Avatar of RossWilko
RossWilko

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial