Link to home
Start Free TrialLog in
Avatar of Roshan Mohammed
Roshan MohammedFlag for New Zealand

asked on

directories,subdirectories,files..listing with vb.net

Is it  possible to list the contents (directories,subdirectories, and files) of a parent directory folder, with vb.net

Then for each subdirectory (becomes a parent), it's files and subdirectories, until for each subdirectory, it's subdirectories, and files are listed out..

I have tried with numerious attempts, but being a vb.net newbie, i am having all sorts of errors an all, so i need your
(expert) help.

S00007359

:-(
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

This code will list all the files/folders in a directory and will make a file of this list:

For subfolders you need to have a recursive function (GetFiles() list after this event)

'***********************

  Dim TargetFileText As String ' dEclare it outside any function

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Sourcedir As String = "c:\windows"   ' the folder to get the list of files/subfolders
        GetFiles(Sourcedir) 'USER DEFINED FUNCTION

        Dim TargetFile As File
        Dim writer As StreamWriter

        If TargetFile.Exists("C:\Temp\List.txt") Then
            writer = TargetFile.AppendText("C:\Temp\List.txt")
        Else
            writer = TargetFile.CreateText("C:\Temp\List.txt")
        End If

        writer.WriteLine(TargetFileText)
        writer.Close()
    End Sub

'THIS FUNCTION WILL RECUSRSIVELY FIND FILES IN PARTICULAR FOLDER
Private Function GetFiles(ByVal DirName As String)

        Dim d As DirectoryInfo = New DirectoryInfo(DirName)

        Dim f() As FileInfo = d.GetFiles()

        TargetFileText += vbCrLf & "-------------------------------------------------" & vbCrLf
        TargetFileText += "Directory: " & d.FullName & vbCrLf
        TargetFileText += "Name" & vbTab & vbTab & "Bytes" & vbTab & "Last Modified" & vbCrLf

        Dim fi As FileInfo

        For Each fi In f
            TargetFileText += fi.Name & vbTab & fi.Length & vbTab & fi.LastWriteTime & vbCrLf
        Next

        Dim innerFolders As DirectoryInfo

        For Each innerFolders In d.GetDirectories()
            GetFiles(innerFolders.FullName)
        Next
 End Function

'***********************

Cheers!
Avatar of Roshan Mohammed

ASKER

The following does the trick, but how To recurse or make it to loop as long as each folder has subfolders, files, "AND" subfolders, in it, and so forth.Basically find out all files and folders..

Can someone please help me out here!



      Sub ShowTempFiles()
        Dim FolderExists As Boolean
        Dim File As System.IO.File
        Dim FileInfo As System.IO.FileSystemInfo
        Dim DirFolder As System.IO.Directory
        Dim Dir As System.IO.DirectoryInfo
        Dim DirInfo As System.IO.DirectoryInfo

        Dim NoDirs As Integer()


        FolderExists = System.IO.Directory.Exists("C:\Temp")
        DirInfo = New System.IO.DirectoryInfo("C:\Temp")

        REM For Each FileInfo In DirInfo.GetFiles("*,*")
        REM Debug.WriteLine(FileInfo.FullName.ToString)
        REM Next


        If FolderExists = False Then
            System.IO.Directory.CreateDirectory("C:\Temp")
        Else
            DirInfo = New System.IO.DirectoryInfo("C:\Temp")
            For Each FileInfo In DirInfo.GetFiles("*.*")
               
                Debug.WriteLine(FileInfo.FullName.ToString)
                strMessage = strMessage & vbNewLine & FileInfo.FullName.ToString

            Next
            For Each Dir In DirInfo.GetDirectories("*.*")
                             

                DirInfo = New System.IO.DirectoryInfo(Dir.FullName)
                For Each FileInfo In DirInfo.GetFiles("*.*")
                   
                    Debug.WriteLine(FileInfo.FullName.ToString)
                    strMessage = strMessage & vbNewLine & FileInfo.FullName.ToString

                Next
               
                Debug.WriteLine(Dir.FullName.ToString)

       
            Next
        End If

        End Sub
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

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
Hey Desp !
thanks for the code....