List All Files in Folder plus Subfolders (Simple, One Function)
Given a pathname, this function will return a string containing a list of all files in that folder plus subfolders. Much easier than other examples posted here! A single recursive function, with no API's or special types needed.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
Private Function FileList(ByVal Pathname As String, Optional DirCount As Long, Optional FileCount As Long) As String
'Returns a string containing all files
'at this directory level and lower.
'Example of usage:
' RichTextBox1.Text = FileList("c:\windows")
Dim ShortName As String, LongName As String
Dim NextDir As String
Static FolderList As Collection
Screen.MousePointer = vbHourglass
'First time through only, create collection
'to hold folders waiting to be processed.
If FolderList Is Nothing Then
Set FolderList = New Collection
FolderList.Add Pathname
DirCount = 0
FileCount = 0
End If
Do
'Obtain next directory from list
NextDir = FolderList.item(1)
'Remove next directory from list
FolderList.Remove 1
'List files in directory
ShortName = Dir(NextDir & "\*.*", vbNormal Or _
vbArchive Or _
vbDirectory)
Do While ShortName > ""
If ShortName = "." Or ShortName = ".." Then
'skip it
Else
'process it
LongName = NextDir & "\" & ShortName
If (GetAttr(LongName) And vbDirectory) > 0 Then
'it's a directory - add it to the list of directories to process
FolderList.Add LongName
DirCount = DirCount + 1
Else
'it's a file - add it to the list of files.
FileList = FileList & LongName & vbCrLf
FileCount = FileCount + 1
End If
End If
ShortName = Dir()
Loop
Loop Until FolderList.Count = 0
Screen.MousePointer = vbNormal
End Function
Original Comments (3)
Recovered from Wayback Machine