Solved

Get all files in Folder and all subfolders

Posted on 2006-10-25
4
518 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 63

Expert Comment

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

Accepted Solution

by:
gangwisch earned 300 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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 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 63

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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: …

628 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