Forum Discussion
jukhamil
Sep 30, 2021Brass Contributor
Print files in directory with Dir()
I am trying to print all the files in a directory with the Dir() function but it isn't working as expected. It usually either returns a blank string or just the name of the directory I passed in the ...
HansVogelaar
Sep 30, 2021MVP
For example:
Sub Example()
Call ListFiles("C:\Users\juhamilton\Desktop")
End Sub
Sub ListFiles(ByVal sPath As String)
Dim sFile As String
If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
End If
sFile = Dir(sPath & "*.*")
Do While sFile <> ""
Debug.Print sFile
sFile = Dir
Loop
End Sub