Fog Creek Software
g
Discussion Board




VB: How do I recursively display all files

Not sure what the VB objects are.  Just want to write a routine that will recusively display all files on the local hard drive.

*Null
Thursday, May 20, 2004

Come on - do some research before you post !!!

This is a pretty basic problem and there is sample code everywhere if you looked.

duh!!
Thursday, May 20, 2004

VB has built-in commands to do this, but it's much easier to use the FileSystemObjects.  There, that'll give you something to Google on.  (=

Sam Livingston-Gray
Thursday, May 20, 2004

Give the guy a break.
"vb recursive files" only comes up with 16k+ files ... hard to find anything meaningful in that small a return.
"vb dir files" comes up with almost 64k hits but you'd have to have looked in the help file and found the Dir command to know to use it.

Scoop
Thursday, May 20, 2004

*Null,

http://www.google.com.au/search?q=VB+How+do+I+recursively+display+all+files&ie=UTF-8&hl=en&btnG=Google+Search&meta=

Seeya

Anon
Thursday, May 20, 2004

In life there are people who manage to tie their own shoe laces by the time they're 5 or so, and there are people who need to ask everyone else where their shoe laces are and why they won't fit in their pumps.

Simon Lucy
Friday, May 21, 2004

Geezz...lets just write it here..in line as air code!!

As always..the answer lies in “how” you want to display the results.

As others have commented…using the file system object is usually preferred.

The other problem is that the VB dir() does not hold its state…you have to exhaust it each time. The other problem is that dir() can return file names without atrribles..and it thinks it is directory (that gives a exection error). So, for quick and dirty...on error just exits...

This code snip sends the results to collection. What you do with that collection after is up to you. This test snip sends the results to the debug window. You can run the following code in Excel, Word..or even fire up VB if you want….

Sub dirTest()

  Dim dlist        As New Collection
  Dim startDir    As String
  Dim i              As Integer

  startDir = "C:\Program files\"
  Call FillDir(startDir, dlist)

  MsgBox "there are " & dlist.Count & " in the dir"

  ' lets printout the stuff into debug window for a test

  For i = 1 To dlist.Count
      Debug.Print dlist(i)
  Next i

End Sub


Sub FillDir(startDir As String, dlist As Collection)

  ' build up a list of files, and then
  ' add add to this list, any additinal
  ' folders

  Dim strTemp              As String
  Dim colFolders          As New Collection
  Dim vFolderName      As Variant

  On Error GoTo bailout
     
  strTemp = Dir(startDir)

  Do While strTemp <> ""
      dlist.Add startDir & strTemp
      strTemp = Dir
  Loop

  ' now build a list of additional folders
  strTemp = Dir(startDir & "*.", vbDirectory)

  Do While strTemp <> ""
      If (strTemp <> ".") And (strTemp <> "..") Then
        colFolders.Add strTemp
      End If
      strTemp = Dir
  Loop

  ' now process each folder (recursion)
  For Each vFolderName In colFolders
      Call FillDir(startDir & vFolderName & "\", dlist)
  Next vFolderName
 
bailout:
 
 
End Sub


Albert D. Kallal
Edmonton, Alberta Canada
kallal@msn.com
http://www.attcanada.net/~kallal.msn

Albert D. Kallal
Friday, May 21, 2004

Simon, you magnificent bastard, your post was magnificent....in a bastardly way. :-P

Bobby Frost
Saturday, May 22, 2004

You can accomplish the same thing in a lot less code by referencing the Microsoft Scripting Runtime and using the FileSystemObject:

'*********************************
Dim fso As New Scripting.FileSystemObject

Private Sub Form_Load()
    Call showfiles(fso.GetFolder("C:\"))
End Sub

Sub showfiles(ByRef ThisFolder As Folder)
    Dim ThisFile As File
   
    ' List files in this folder
    For Each ThisFile In ThisFolder.Files
        Debug.Print ThisFile.Path & "\" & ThisFile.Name
    Next

    ' Loop through subdirectories
    For Each ThisFolder In ThisFolder.SubFolders
        Call showfiles(ThisFolder)
    Next
End Sub

Paul S
Saturday, May 22, 2004

"I can code a solution to that problem in 30 lines."

"I can code a solution to that problem in 20 lines."

"I can code a solution to that problem in 15 lines."

"Code that solution."

Wisea**
Sunday, May 23, 2004

*  Recent Topics

*  Fog Creek Home