Solved

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

Posted on 2004-04-07
5
4,984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Part One of the two-part Q&A series with MalwareTech.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

624 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