Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2003

Tip: Looking for answers? Try searching our database.

count directories

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Torstein S. Johnsen - 30 Oct 2003 10:06 GMT
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
Jay Freedman - 31 Oct 2003 02:16 GMT
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

Signature

Regards,
Jay Freedman
Microsoft Word MVP     FAQ: http://www.mvps.org/word

> 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
[quoted text clipped - 7 lines]
>
> Torstein S. Johnsen
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.