count directories

  • Thread starter Torstein S. Johnsen
  • Start date
T

Torstein S. Johnsen

This seems to me as a simple question, but as a newbie I'm not able to
figure out how to count all subdirectories directly under a given directory.

I have a directory called N:\bedrift which has several subdirectories
N:\bedrift\abb, N:\bedrift\aateigen\ and so on. I like to count them all.

Thanks

Torstein S. Johnsen
 
J

Jay Freedman

Hi, Torstein,

Here's a function that returns the number of subdirectories by using the
FileSystemObject, along with a simple subroutine that shows how to use it:

Function CountSubFolders(FolderSpec As String) As Long
Dim oFileSystem, oFolder, FolderCollection
Dim oSubFolder

Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFileSystem.GetFolder(FolderSpec)
Set FolderCollection = oFolder.SubFolders
CountSubFolders = FolderCollection.Count
End Function

Sub test_it()
Dim c As Long
Dim sPath As String

With Dialogs(wdDialogFileOpen)
Do While .Display = -1 ' until canceled
' get path without file name
sPath = WordBasic.FileNameInfo(.Name, 5)

c = CountSubFolders(sPath)

MsgBox sPath & " contains " & c & " subfolders"
Loop
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top