Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get all files in Folder and all subfolders

Posted on 2006-10-25
4
Medium Priority
?
521 Views
Last Modified: 2010-04-23
Hello

How can I get all files in a folder and in all subfolders of a folder in a listview?

Thanks

Sven
0
Comment
Question by:Ruttensoft
  • 3
4 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17805551
What version of VB .Net 2003 or 2005?
0
 
LVL 10

Accepted Solution

by:
gangwisch earned 1200 total points
ID: 17805764
Private Sub ShowFiles()
  Dim d() As String

  d = System.IO.Directory.GetFiles("c:\")

  Dim en As System.Collections.IEnumerator

  en = d.GetEnumerator

  While en.MoveNext
     Console.WriteLine(CStr(en.Current)) 'en.current will hold the link to your current file
  End While
End Sub
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 total points
ID: 17805913
Hi Ruttensoft;

In VB 2005 you can get a list of all files including subdirectories and place them all into a ListView cvontrol as follows.

        Dim Dir As String = "Directory Path To Search"
        Dim files() As String = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories)
        Dim lvi(files.Length - 1) As ListViewItem
        Dim idx As Integer = 0

        For Each file As String In files
            lvi(idx) = New ListViewItem(file)
            idx += 1
        Next
        If lvi.Length > 0 Then
            ListView1.Items.AddRange(lvi)
        End If

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17806327
Hi Ruttensoft;

I want to make sure I did not misunderstand the question. Your question was "How can I get all files in a folder AND IN ALL SUBFOLDERS of a folder in a LISTVIEW?". If that was the question how does the solution that gangwisch provided do that?

My last post was for VB 2005 the below code will work for VB 2003.

    ' Class level variable
    Private fileNames As New ArrayList

To get ALL files

        Dim Dir As String = "Directory path to the Folder"

        GetAllFiles(Dir)

        Dim lvi(fileNames.Count - 1) As ListViewItem
        Dim idx As Integer = 0

        For Each file As String In fileNames
            lvi(idx) = New ListViewItem(file)
            idx += 1
        Next
        If lvi.Length > 0 Then
            ListView1.Items.AddRange(lvi)
        End If



    Private Sub GetAllFiles(ByVal dir As String)

        Dim curInfo As New DirectoryInfo(dir)
        Dim dis() As DirectoryInfo = curInfo.GetDirectories
        Dim files() As FileInfo = curInfo.GetFiles

        For Each fi As FileInfo In files
            fileNames.Add(fi.FullName)
        Next

        For Each di As DirectoryInfo In dis
            GetAllFiles(di.FullName)
        Next

    End Sub


Fernando
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

916 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