We help IT Professionals succeed at work.
Get Started

VBS Script to List All Folders and Sub Folders

Thomas Grassi
on
4,152 Views
Last Modified: 2014-05-04
Trying to list all folders and sub folders only of a main folder. do not want the files included in this list.

I found this but can not get it to run

Dim Mypath as string, MyName as String,iCount as integer
iCount=0
MyPath = "M:\MUSIC\MP#MUSICALBUMS"   ' Set the path.
MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
Do While MyName <> ""   ' Start the loop.
   ' Ignore the current directory and the encompassing directory.
   If MyName <> "." And MyName <> ".." Then
      ' Use bitwise comparison to make sure MyName is a directory.
      If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
         Debug.Print MyName   ' Display entry only if it
         iCount=iCount+1
     End If   ' it represents a directory.
   End If
   MyName = Dir   ' Get next entry.
Loop
debug.print "No.of Folders in the selected path : " & iCount

Open in new window



also tried this one

Sub Ck()     
    Dim strStartPath As String     
    strStartPath = "M:\MUSIC\MP3MUSICALBUMS" 'ENTER YOUR START FOLDER HERE
    ListFolder strStartPath     
End Sub 
Sub ListFolder(sFolderPath As String)     
    Dim FS As New FileSystemObject 
    Dim FSfolder As Folder 
    Dim subfolder As Folder 
    Dim i As Integer     
    Set FSfolder = FS.GetFolder(sFolderPath)     
    For Each subfolder In FSfolder.SubFolders 
        DoEvents 
        i = i + 1 
         'added this line
        Cells(i, 1) = subfolder 
         'commented out this one
         'Debug.Print subfolder
    Next subfolder     
    Set FSfolder = Nothing     
     'optional, I suppose
    MsgBox "Total sub folders in " & sFolderPath & " : " & i     
End Sub 

Open in new window




Sample one fails unexpected end of statement line 1 char 12

Sample two fails unexpected end of statement line 2 char 22
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 24 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE