UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> CountFolders    


Contents

Synopsis

Count the number of folders in a specified path.

CODE
' CountFolders
' http://www.utteraccess.com/wiki/CountFolders
' 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_GetCountFolders
'*************** Code Start *****************************************************
' Purpose  : Count the number of subfolders in a folder given a path
' Return   : Long
'--------------------------------------------------------------------------------
'                              GetCountFolders
'--------------------------------------------------------------------------------
Function GetCountFolders(psPath As String) As Long
'strive4peace
'uses Late Binding. Reference for Early Binding:
'  Microsoft Scripting Runtime
  'PARAMETER
  '  psPath is path to get the number of folders for
  '     for example, c:\myPath
  ' Return: Long
  '  -1 = path not valid
  '   0 = no folders found, but path is valid
  '  99 = number of folders where 99 is some number
 
  'inialize return value
  GetCountFolders = -1
  'skip errors    
  On Error Resume Next
  'count SubFolders in FileSystemObject for psPath  
  With CreateObject("Scripting.FileSystemObject")
     GetCountFolders = .GetFolder(psPath).SubFolders.Count
  End With
End Function
'*************** Code End *******************************************************

'--------------------------------------------------------------------------------
'                              call_GetCountFolders_MsgBox
'--------------------------------------------------------------------------------
Sub call_GetCountFolders_MsgBox ()
  'click HERE and press F5 to run
  Dim sPath As String
  sPath = "c:\myPath"    '------------- customize
  MsgBox Format(GetCountFolders(sPath), "#,##0") _
     & " folders in " & sPath _
     , , "GetCountFolders"
End Sub
'--------------------------------------------------------------------------------
'                              call_GetCountFolders_BadPath
'--------------------------------------------------------------------------------
Sub call_GetCountFolders_BadPath ()
  'click HERE and press F5 to run
  Dim sPath As String _
     , nCount As Long
     
  sPath = "c:\invalid"
  nCount = GetCountFolders(sPath)
 
  If nCount < 0 Then
     MsgBox sPath & " is not valid" _
        , , "GetCountFolders"
  Else
     MsgBox Format(nCount, "#,##0") _
        & " folders in " & sPath _
        , , "GetCountFolders"
  End If
End Sub

Creative Commons License
CountFolders 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 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 folders using the .SubFolders.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 folders (Long)

Related code is CountFiles

Parameter

  • psPath is the path to count folders in such as c:\myPath

Uses

If you are iterating through files, this can let you know if there are also subfolders to read. Perhaps you are writing your own file browser to restrict access to certain locations.

Edit Discussion
Custom Search


Thank you for your support!
This page has been accessed 128 times.  This page was last modified 18:08, 22 July 2019 by Strive4peace.   Disclaimers