Solved

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

Posted on 2004-04-07
5
4,978 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

830 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