views:

92

answers:

5

in vba how do i get a list of all the files with a specific extension in a specific directory?

i am unable to do application.filesearch, because i am using excel 2007

+1  A: 

Stupid MS....

Check http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/a450830d-4fc3-4f4e-aee2-03f7994369d6 for a discussion of the issue.

KevenDenen
as stated in the question i am unable to do application.filesearch, because i am using excel 2007
I__
Ugh, I didn't realize MS killed application.filesearch in Excel 2007. WTF Microsoft??
KevenDenen
ahahahahahahahhhaha
I__
In regard to the FileSearch object, I've created a class module that is an effort to replace its core functionality: http://dfenton.com/DFA/download/Access/FileSearch.html . It still needs work, particular in order to work on Vista/Win7 when attempting to search restricted directories (which in the current version produces an error).
David-W-Fenton
+1  A: 

Dir("C:\yourPath\*.ESY", vbNormal) Returns the first file with esy extension. Each subsequent call to Dir() returns the next.

mohnston
great, so how many times do i know to run it?
I__
Test the length of the result in a WHILE or DO loop.When length is 0 you are done.
mohnston
+3  A: 

In response to your comment "great, so how many times do i know to run it?" ... this example runs until it lists all the files which match strPattern. Change the strFolder constant.

Public Sub ListESY()
Const strFolder As String = "C:\SomeFolder\"
Const strPattern As String = "*.ESY"
Dim strFile As String
strFile = Dir(strFolder & strPattern, vbNormal)
Do While Len(strFile) > 0
    Debug.Print strFile
    strFile = Dir
Loop
End Sub
HansUp
+1  A: 

Alternative option: use the "Microsoft Scripting Runtime" library (check it in Tools...References) for the FileSystemObject family of objects. Something like the following, perhaps:

Public Function ESYFileCount(dir_path as String) as Long

Dim fil As File

    With New FileSystemObject
        With .GetFolder(dir_path)
            For Each fil In .Files
                If LCase(Right(fil.Name, 4)) = ".esy" Then
                    ESYFileCount = ESYFileCount + 1
                End If
            Next
        End With        
    End With

End Function
Mike Woodhouse
This code would be better (and earn a +1 from me) if it used late binding instead of requiring a reference to the FSO.
David-W-Fenton
@David-W-Fenton - I don't understand why late binding would be better, care to elucidate?
Mike Woodhouse
Late binding is better because of the fact that automation of the FSO can be blocked by domain policies. Late binding is *always* better for any component that is not part of the default Access set of references (with very few exceptions). The performance hit is easily avoided by caching a reference to it, and using that instead of re-initializing it each time you use it.
David-W-Fenton
mike you have been officially 0wned
I__
+2  A: 

Allen Browne has some good code on this that does more than you asked for:

List files recursively

David-W-Fenton