Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordsWithout 49 79
mapAB Challlenge 35 85
topping3 challenge 14 49
wordlen challenge 3 46
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now