Looping through files in a directory

rberkers

Registered User.
Local time
Today, 20:28
Joined
Sep 16, 2008
Messages
14
Hi,

If have written a small VBA program that shows all the files in a directory and it works as a charm:

Code:
Public Sub LoopFiles()

    Dim strPath As String

    Dim objFso As FileSystemObject
    Dim objFolder As Folder
    Dim objFile As File
    
    strPath = "C:\Windows"

    Set objFso = New FileSystemObject
    Set objFolder = objFso.GetFolder(strPath)
    
    For Each objFile In objFolder.Files
        Debug.Print objFile.Name
    Next
        
End Sub
What I would like that it only shows the files of a certain filemask. Is it possible to use the FileSystemObject to get only the files of a certain filemask and loop through them?

Thanks in advance.

Regards,
Rob.
 
Maybe something like

Code:
        If objFile.name Like "*.dll" Then
            Debug.Print objFile.name
        End If
of course you could make the "*.dll" a parameter to the function rather than hard-coding it but you get the idea.
 
Any reason why you want to use the FileSystemObject library?

Code:
Public Sub LoopFiles()
    Dim strPath As String
    Dim strFilename As String
    
    strPath = "C:\Windows\*.dll"
    
    strFilename = Dir(strPath)
    Do Until strFilename = ""
        Debug.Print strFilename
        strFilename = Dir
    Loop
        
End Sub
 
Any reason why you want to use the FileSystemObject library?

I was told that in case of a directory with a huge amount of file-entries the FileSystemObject would be faster as use "DIR".

Regards,
Rob.
 

Users who are viewing this thread

Back
Top Bottom