Solved

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

Posted on 2004-04-07
5
4,976 Views
Last Modified: 2010-06-22
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

:-(
0
Comment
Question by:S00007359
5 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 10781093
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!
0
 
LVL 12

Author Comment

by:S00007359
ID: 10809353
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
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 11075472
PAQed, with points refunded (60)

Computer101
E-E Admin
0
 

Expert Comment

by:therock_
ID: 20028565
Hey Desp !
thanks for the code....
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question