PDA

View Full Version : Solved: VBA count files funtion



SBrooky
07-31-2012, 07:57 AM
Hello, Ive found this function online and I want to change it so that it just counts .exe files as oppose to any file extention. Any ideas:
Function FlrFileCount(sFileFlr As String) As Long
On Error GoTo Error_Handler
Dim fso As Object
Dim flr As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set flr = fso.GetFolder(sFileFlr)

FlrFileCount = flr.Files.Count

Error_Handler_Exit:
On Error Resume Next
Set flr = Nothing
Set fso = Nothing
Exit Function

Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: FlrFileCount" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function

Bob Phillips
07-31-2012, 09:07 AM
Function FlrFileCount(sFileFlr As String, Optional FileType As String = "exe") As Long
On Error GoTo Error_Handler
Dim fso As Object
Dim flr As Object
Dim file As Object
Dim i As Long

Set fso = CreateObject("Scripting.FileSystemObject")
Set flr = fso.GetFolder(sFileFlr)

For Each file In flr.Files

FlrFileCount = FlrFileCount - (file.Name Like "*." & FileType & "*")
Next file

Error_Handler_Exit:
On Error Resume Next
Set flr = Nothing
Set fso = Nothing
Exit Function

Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: FlrFileCount" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function

SBrooky
07-31-2012, 09:31 AM
Perfect! Thanks alot =)