Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get full file paths of a directory 1

Status
Not open for further replies.

Horowitz

Programmer
Jan 29, 2005
30
GR
hi,

I want to add to a listbox the full paths of the files in a directory AND the files in subdirectories.
e.g : c:\test\abc.xls
c:\test\aaa.exe
c:\test\1\2\asd.doc
...
(the folder "c:\test\1" has no files in it)

Tried this:

Code:
    Dim dirs() As String = Directory.GetDirectories("c:\test")
        For Each dir As String In dirs
            Dim dirs1() As String = Directory.GetFiles(dir)
            For Each dir1 As String In dirs1
                ListBox1.Items.Add(dir1)
            Next
        Next

It hasn't the result I want because it checks the DIRECTORIES only in "c:\test". If there is a file in "c:\test\asd.exe";it won't be added to the listbox.

Any ideas?
 
you could simply add:

for each dir1 as string in directory.getfiles("c:\test")
listbox1.items.add(dir1)
next

to the end of your code.

-Rick

----------------------
 
I have tried this u wrote me but the results aren't what i want. I'll retype my question.

How can I add to a listbox all the files in directory "c:\test" with the full path. There are files in subdirectories.
eg: c:\test\asd.exe
c:\test\1\2\3\4\5\asd.exe
c:\test\test1\test2\asd.exe e.t.c.

Now better?
 
I went waaay over time on this one. But It frustrated me, so I had to finish it. This is a recursive file list generator. It will return all of the files (with paths) in a give folder, and all sub folders.

Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim List As Collection = RecursiveDirectoryList("c:\test")
    Dim objItem As Object

    Me.ListBox1.Items.Clear()
    For Each objItem In List
      Me.ListBox1.Items.Add(objItem.ToString)
    Next
  End Sub

  Public Function RecursiveDirectoryList(ByVal path As String) As Collection
    Dim dirs() As String = System.IO.Directory.GetDirectories(path)
    Dim dir As String
    Dim ReturnFiles As New Collection()
    Dim Files As Collection
    Dim sFiles() As String
    Dim objFile As Object
    For Each dir In dirs
      Files = RecursiveDirectoryList(dir)
      For Each objFile In Files
        ReturnFiles.Add(objFile)
      Next
    Next

    sFiles = System.IO.Directory.GetFiles(path)
    For Each objFile In sFiles
      ReturnFiles.Add(objFile)
    Next
    Return ReturnFiles
  End Function

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top