The code below will demonstrate a class that will provide the file names for a specified folder and file filter. Create a new project with a form. Add a Class module to the project. Copy the code below and paste it into the created Class. Save it with the name Files. You will need to set the Name property of the class to Files.
Next create two buttons, name them cmdSample1 and cmdSample2. Then copy the code below the Class (scroll down), and paste it into the code window of your form.
Link to the files -->> Files.cls, form1.frm
Click run to try it out.
Class code below
Option Explicit
' Class Files
'
' File List
'
' This object returns the files names in a directory
'
' The application directory is the default
' All files *.* is the default file filter
'
' getCount() returns the number of file names available
' getNextName() returns the next file name
' setDirectory(string) sets the directory
' setFileFilter(string) set the filter
Private theFiles As Collection
Private currentDir As String
Private fileFilter As String
Private loaded As Boolean
Private currentName As Integer
Public Function getCount() As Integer
If Not loaded Then
readFiles
End If
getCount = theFiles.Count
End Function
Public Function getNextName() As String
If Not loaded Then
readFiles
End If
If currentName = 0 Then
'raise error
getNextName = ""
Else
getNextName = theFiles.item(currentName)
currentName = currentName + 1
If currentName > theFiles.Count Then
currentName = 1
End If
End If
End Function
Public Sub setDirectory(ByVal theDir As String)
If Right$(theDir, 1) <> "\" Then
theDir = theDir & "\"
End If
currentDir = theDir
clearCollection
End Sub
Public Sub setFilter(ByVal theFilter As String)
fileFilter = theFilter
clearCollection
End Sub
Private Sub clearCollection()
Dim i As Integer
For i = 1 To theFiles.Count
theFiles.Remove (1)
Next i
loaded = False
End Sub
Private Sub readFiles()
Dim fileName As String
fileName = Dir(currentDir & fileFilter)
Do Until fileName = ""
theFiles.Add fileName
fileName = Dir
Loop
loaded = True
If theFiles.Count > 0 Then
currentName = 1
Else
currentName = 0
End If
End Sub
Private Sub Class_Initialize()
Set theFiles = New Collection
currentDir = App.Path & "\"
fileFilter = "*.*"
readFiles
End Sub
Form Code below
Option Explicit
' Form code
Const START_SIZE As Integer = 20
Private Sub cmdSample1_Click()
Dim fileName As String
Dim Files() As String
Dim counter As Integer
counter = 0
ReDim Files(START_SIZE)
'fileName = Dir(App.Path & "\" & "*.txt")
fileName = Dir("C:\" + "*.SYS", vbHidden + vbNormal + vbReadOnly + vbSystem)
Do Until fileName = ""
counter = counter + 1
Files(counter) = fileName
fileName = Dir
Loop
Dim i As Integer
For i = 1 To counter
Print Files(i)
Next i
End Sub
Private Sub cmdSample2_Click()
Dim fileList As Files
Set fileList = New Files
MsgBox fileList.getCount
Dim i As Integer
For i = 1 To fileList.getCount
Print fileList.getNextName
Next i
fileList.setFilter ("*.txt")
MsgBox fileList.getCount
Cls
For i = 1 To fileList.getCount
Print fileList.getNextName
Next i
fileList.setDirectory ("C:")
fileList.setFilter ("*.sys")
MsgBox fileList.getCount
Cls
For i = 1 To fileList.getCount
Print fileList.getNextName
Next i
End Sub