Hi Everyone,
I am counting files using below code and I want to show how many files counted in system status bar using syscmd acsyscmdsetstatus. However, as this is For Each loop when I use syscmd acsyscmdsetstatus it resets to 0 for each sub folder. How should I modify the code to keep counting files and not reset to 0? I could use something like gCount = gCount + 1, acsyscmdsetstatus, gCount with "Do loop" but for "for each" it doesn't work.
I am counting files using below code and I want to show how many files counted in system status bar using syscmd acsyscmdsetstatus. However, as this is For Each loop when I use syscmd acsyscmdsetstatus it resets to 0 for each sub folder. How should I modify the code to keep counting files and not reset to 0? I could use something like gCount = gCount + 1, acsyscmdsetstatus, gCount with "Do loop" but for "for each" it doesn't work.
Code:
Private Function CountFiles_FolderAndSubFolders(strFolder As String, Optional strExt As String) As Double
'Author : Ken Puls
'Function purpose: To count files in a folder and all subfolders. 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 objSubFolder As Object
Dim objSubFolders 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(strFolder).Files
Set objSubFolders = objFso.GetFolder(strFolder).SubFolders
'Count files (that match the extension if provided)
If strExt = "All" Then
CountFiles_FolderAndSubFolders = objFiles.count
Else
For Each objFile In objFiles
If UCase(Right(objFile.path, (Len(objFile.path) - InStrRev(objFile.path, ".")))) = UCase(strExt) Then
CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + 1
End If
Next objFile
End If
'Request count of files in subfolders
'Do
SysCmd acSysCmdSetStatus, CountFiles_FolderAndSubFolders
For Each objSubFolder In objSubFolders
CountFiles_FolderAndSubFolders = CountFiles_FolderAndSubFolders + _
CountFiles_FolderAndSubFolders(objSubFolder.path, strExt)
Next objSubFolder
DoEvents
'Loop While objSubFolder <> vbNullString
EarlyExit:
'Clean up
On Error Resume Next
Set objFile = Nothing
Set objFiles = Nothing
Set objFso = Nothing