Archive

Tuesday, August 2, 2011

Get count of file with matching extension in a Folder

In automation testing, you might come across with situation where in you need to find out number of files with a particular extension. Below is the code to get the count of file matches with the extension.

Function GetFileCount(ByVal sFolderPath, ByVal sExt)

 

            Dim iCount

            Dim oFile, pFolder

            Dim fso

            On Error Resume Next

           

            Set fso=CreateObject("Scripting.FileSystemObject")

            set pFolder=fso.GetFolder(sFolderPath)

            set oFile=pFolder.Files

 

            For each sFile in oFile

                        If instr(1,lcase(sFile.Path),sExt,1)>0 Then     'It's a text comparison

                                    iCount=iCount+1

                        End IF

            Next

 

                        'Count all files in each subfolder – recursion point

            For Each SubFld In pFolder.SubFolders

                        iCount = iCount + GetFilecount(SubFld.Path, sExt)

            Next

 

            GetFilecount=iCount  'Return the count to the calling statement

            Set fso=Nothing

End Function

 

sPath="E:\Debajit Hazarika\EDI Files"

sCount=GetFileCount(sPath, ".mbi")

Msgbox "Total Count  ="&sCount


No comments: