Directory Count 
Author Message
 Directory Count

Normally, in VB, I could add a DirListBox and do a count to return the
number of directories under a parent Dir. How can this be accomplished in
VBA (access), which doesn't have a DirListBox object? Can I use the
filesystemobject? I didn't see that had a suitable method.

--
Steve Wasser
http://www.*-*-*.com/
the journal of contrarian social discourse and neurotic opinion



Sat, 13 Aug 2005 03:37:15 GMT  
 Directory Count
Hi Steve,

I took the following from the article "How to get the names of all the
folders in the folder tree, starting from a specified folder" at
http://www.mvps.org/word/FAQs/MacrosVBA/ReadFoldersIntoArray.htm.

Dim FoldersArray As Variant
Dim i As Integer

'Read all subfolders of the specified folder into an array
'by calling the funcGetSubfolders function
FoldersArray = funcGetSubfolders("C:\Temp\Test\")

Debug.Print UBound(FoldersArray)
End Sub

Public Function funcGetSubfolders(ByVal FolderToRead As String) As Variant

'This function uses a string as a parameter and not an array.
'It translates this string to an array and then starts the main function,
'funcGetAllSubfolders'

Dim AllSubFolders(0) As Variant

On Error Resume Next
System.Cursor = wdCursorWait

'Add a backslash to  the end of the path, if not there already
If (Right$(FolderToRead, 1) <> "\") Then
    FolderToRead = FolderToRead & "\"
End If

'Set the path as the first entry in the array and pas the array to the main
function
AllSubFolders(0) = FolderToRead
funcGetSubfolders = funcGetAllSubfolders(AllSubFolders)

System.Cursor = wdCursorNormal
StatusBar = ""
On Error GoTo 0

End Function

Quote:
> Normally, in VB, I could add a DirListBox and do a count to return the
> number of directories under a parent Dir. How can this be accomplished in
> VBA (access), which doesn't have a DirListBox object? Can I use the
> filesystemobject? I didn't see that had a suitable method.

> --
> Steve Wasser
> http://xdissent.com
> the journal of contrarian social discourse and neurotic opinion



Sat, 13 Aug 2005 04:25:22 GMT  
 Directory Count
Hi Steve,

Quote:
> VBA (access), which doesn't have a DirListBox object? Can I use the
> filesystemobject? I didn't see that had a suitable method.

Function FolderCount(ByVal Rootfolder As String) As Long
    Dim f As New FileSystemObject
    FolderCount = f.GetFolder(Rootfolder).SubFolders.Count
    Set f = Nothing
End Function

From the imm window:
? FolderCount("c:\temp")
 5

Krgrds,
Perry



Quote:
> Normally, in VB, I could add a DirListBox and do a count to return the
> number of directories under a parent Dir. How can this be accomplished in
> VBA (access), which doesn't have a DirListBox object? Can I use the
> filesystemobject? I didn't see that had a suitable method.

> --
> Steve Wasser
> http://xdissent.com
> the journal of contrarian social discourse and neurotic opinion



Sat, 13 Aug 2005 08:40:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to get file count in directory

2. Counting Files in a Directory

3. Count of folders in directory?

4. counting directory size

5. Count all files in a directory tree

6. Count files in directory ?

7. help: Counting how many files in directory

8. Count files in a directory

9. code for counting files in a directory

10. code for counting files in a directory

11. Directory-getting count and names?

12. Counting files in a directory???

 

 
Powered by phpBB® Forum Software