Count files in a folder

Lavalamp

New member
Joined
Apr 9, 2014
Messages
2
Reaction score
0
Points
0
I've been using the code from the knowledge base here and have had great success on my PC.

But I've now switched to a mac and cannot figure out how to make it work on there. I've obviously changed the directory path, but the cell stays blank instead of having the number.

This is the code I have now:

Code:
Private Function CountFiles(strDirectory As String, Optional strExt As String = "*.*") As Double
'Author : Ken Puls (website)
'Function purpose: To count files in a directory. If a file extension is provided,
' then count only files of that type, otherwise return a count of all files.




Dim objFso As Object
Dim objFiles As Object
Dim objFile As Object


'Set Error Handling
On Error GoTo EarlyExit


'Create objects to get a count of files in the directory
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(strDirectory).Files


'Count files (that match the extension if provided)
If strExt = "*.*" Then
CountFiles = objFiles.Count
Else
For Each objFile In objFiles
If UCase(Right(objFile.Path, (Len(objFile.Path) - InStrRev(objFile.Path, ".")))) = UCase(strExt) Then
CountFiles = CountFiles + 1
End If
Next objFile
End If


EarlyExit:
'Clean up
On Error Resume Next
Set objFile = Nothing
Set objFiles = Nothing
Set objFso = Nothing
On Error GoTo 0


End Function

and the cell content:
Code:
=CountFiles("/Users/users/dropbox/foldertocount")

Any help offered would be greatly appreciated.
 
Comment-out the On Error line, put a breakpoint on the first line of the function (the line that starts Private Function) then step through the code with F8, it will either report an error or just exit the function, but you'll have an idea which line causes the problem.
 
Back
Top