> CountFiles    


Contents

Synopsis

Count the number of files in a specified folder path.

CODE
' CountFiles
' http://www.utteraccess.com/wiki/CountFiles
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2019-07-22                    
'
' module name: mod_File_GetCountFiles
'*************** Code Start *****************************************************
' Purpose  : Count the number of files in a folder
' Return   : Long
'--------------------------------------------------------------------------------
'                              GetCountFiles
'--------------------------------------------------------------------------------
Function GetCountFiles(psPath As String) As Long
'strive4peace
'uses Late Binding. Reference for Early Binding:
'  Microsoft Scripting Runtime
  'PARAMETER
  '  psPath is folder to get the number of files for
  '     for example, c:\myPath
  ' Return: Long
  '    -1 = path not valid
  '     0 = no files found, but path is valid
  '    99 = number of files where 99 is some number
 
  'inialize return value
  GetCountFiles = -1
  'skip errors    
  On Error Resume Next
  'count files in folder of FileSystemObject for path  
  With CreateObject("Scripting.FileSystemObject")
     GetCountFiles = .GetFolder(psPath).Files.Count
  End With
End Function
'*************** Code End *******************************************************
'--------------------------------------------------------------------------------
'                              call_GetCountFiles_MsgBox
'--------------------------------------------------------------------------------
Sub call_GetCountFiles_MsgBox ()
  'click HERE and press F5 to run
  Dim sPath As String
  sPath = "c:\myPath"    '------------- customize
  MsgBox Format(GetCountFiles(sPath), "#,##0") _
     & " files in " & sPath _
     , , "GetCountFiles"
End Sub
'--------------------------------------------------------------------------------
'                              call_GetCountFiles_BadPath
'--------------------------------------------------------------------------------
Sub call_GetCountFiles_BadPath ()
  'click HERE and press F5 to run
  Dim sPath As String _
     , nCount As Long
     
  sPath = "c:\invalid"
  nCount = GetCountFiles(sPath)
 
  If nCount < 0 Then  
     MsgBox sPath & " is not valid" _
        , , "GetCountFiles"
  Else
     MsgBox Format(nCount, "#,##0") _
        & " files in " & sPath _
        , , "GetCountFiles"
  End If
End Sub

Creative Commons License
CountFiles by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Logic

  • Assign the function return value to be -1 to indicate that the passed folder path isn't valid.
  • Skip all errors.
  • Use With to create a temporary instance of FileSystemObject so space for a variable that needs to be released isn't allocated.
  • Late binding is used so that the VBA project doesn't have to reference the Microsoft Scripting Runtime library.
  • Get the number of files using the .Files.Count property of GetFolder for the given path (psPath).
  • Because no storage is set aside for an object variable, there is not one to release.
  • Returns the number of files (Long)

Related code is CountFolders

Parameter

  • psPath is the path of a folder to count files in such as c:\myPath

Uses

Perhaps you want to get the number of files in a folder so you can dimension an array. Or maybe you are looping through a folder and importing files. The number of files to process could be used in a message to the user. Each time through the loop, the message could be changed to also show what number is being processed, and perhaps the file name too.