Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get all files in Folder and all subfolders

Posted on 2006-10-25
4
Medium Priority
?
520 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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