CodeItBetter Programming Another VB Programming Blog

How to list all files in a given directory

Posted on September 30, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'File/Folder Handling - How to list all files in a given directory
Private Sub ListFiles(ByVal sPath As String, ByVal sExt As String)
    Dim sCurDir As String, sFile As String
    Dim colFolders As New Collection
 
    colFolders.Add IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
    Do While colFolders.Count
        sCurDir = colFolders.Item(1)
        colFolders.Remove 1
 
        sFile = Dir$(sCurDir, vbDirectory)
        Do While Len(sFile)
            If Not (sFile = "." Or sFile = "..") Then
                If (GetAttr(sCurDir & sFile) Or vbDirectory) = vbDirectory Then
                    colFolders.Add sCurDir & sFile & "\"
                Else
                    If UCase$(Right$(sFile, Len(sExt) + 1)) = "." & UCase$(sExt) Then
                        Text1.Text = Text1.Text & sCurDir & sFile & vbCrLf
                    End If
                End If
            End If
            sFile = Dir
        Loop
    Loop
End Sub
'How to call this function:
'Call ListFiles("C:\Temp\", "txt")
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.