Count the number of folders in a specified path. Complements GetCountFiles
Subfolders in a folder, and the results of a function that calls GetCountFolders.
If you are iterating through files, this can let you know if there are also subfolders to read.
' module name: mod_File_GetCountFolders '*************** Code Start ***************************************************** ' Purpose : Count the number of subfolders in a folder given a path ' Author : crystal (strive4peace) ' Return : Long ' License : below code ' Code List: www.MsAccessGurus.com/code.htm '--------------------------------------------------------------------------------' 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 ' ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** 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
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.
This VBA can be used in applications other than Access ... Excel, Part, PowerPoint, Project, Visio, ...
Since GetCountFiles was posted, it is also nice to count the folders.
Although the Help article doesn't discuss (as of this writing) releasing object variables when they are declared with Dim and then assigned to the return of CreateObject, is is good practice to Close or Quit anything you Open and assign to an object variable, and then set the object variable to nothing when you're done.
The above code uses With instead of declaring and using an object variable, but is still creating the object for the code to temporarily use. Performance might be better by declaring a module object variable and re-using it. In that case, it should also be released.
Help Article: How To Use FileSystemObject
scrrun.dll defines the FileSystemObject ... the Drive object, the Folder object, the File object, and the TextStreamObject object.
Share with others ...
here's the link to copy:
https://MsAccessGurus.com/VBA/Code/File_CountFolders.htm
It is interesting to hear from you. Was something not clear? Did you find a bug? Is an explanation wrong or not sufficient? Do you want the code do more (there is always more)?
Some of you write to say thanks and tell me what you're doing with Access ... its nice to get an echo back. I want you and others to be good with Access, and other Office applications like Excel, Word, and PowerPoint ... and Windows. Take advantage of the strengths in each to manage your information wisely.
Are you a developer? Do you want to share? Email to ask about getting your pages added to the code index.
When we communicate, collaborate, and appreciate, we all get better.
Thank you.
Email me at info@msAccessGurus