printing file trees

D

Danny J. Lesandrini

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
 
D

Douglas J. Steele

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
 
D

Danny J. Lesandrini

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top