How to get list of all files with ESY extension in a directory? How to get list of all files with ESY extension in a directory? vba vba

How to get list of all files with ESY extension in a directory?


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

Public Sub ListESY()Const strFolder As String = "C:\SomeFolder\"Const strPattern As String = "*.ESY"Dim strFile As StringstrFile = Dir(strFolder & strPattern, vbNormal)Do While Len(strFile) > 0    Debug.Print strFile '<- view this in Immediate window; Ctrl+g will take you there    strFile = DirLoopEnd Sub


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


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 LongDim 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 WithEnd Function