Solved

Get all files in Folder and all subfolders

Posted on 2006-10-25
4
510 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 62

Expert Comment

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

Accepted Solution

by:
gangwisch earned 300 total points
Comment Utility
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

12 Experts available now in Live!

Get 1:1 Help Now