printing file trees

  • Thread starter Thread starter sjohnson
  • Start date Start date
Function PrintFileTree(ByVal sFolder As String)
On Error Resume Next

' If you set a reference to Microsoft Scripting Runtime, you
' can use these declares and the following SET ...
'Dim fso As Scripting.FileSystemObject
'Dim fld As Scripting.Folder
'Dim fil As Scripting.File
'Set fso = New Scripting.FileSystemObject

' Otherwise, use late binding. This code will work without
' setting a reference to the library.
Dim fso, fld, fil
Set fso = CreateObject("Scripting.FileSystemObject")


Set fld = fso.GetFolder(sFolder)
For Each fil In fld.Files
Debug.Print fil.Name
' you can also access modify date, size, etc.
Next

Set fld = nothing
Set fso = Nothing

End Function
 
Sorry to quibble, Danny, but why bother with the overhead (and potential
versioning issues) of FSO for something like this?

Function PrintFileTree(ByVal sFolder As String)
On Error Resume Next

Dim strFolder As String
Dim strFile As String

strFolder = sFolder
If Right$(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If

strFile = Dir(strFolder & "*.*")
Do While Len(strFile) > 0
' This will print just the file name:
Debug.Print strFile
' This will print the full path:
' Debug.Print strFolder & strFile
strFile = Dir
Loop

End Function
 
Ahhh! You mean just use the Dir() function!

What was I thinking? Making life hard. It's like the old
saying for doctors:

When you hear hoofbeats, think horses, not zebras.
 
Back
Top