Solved

VBScript to list subfolders, but only 3 levels deep

Posted on 2006-11-08
3
1,088 Views
Last Modified: 2008-01-16
Hi!

I'm looking for a VBScript that can list all subfolders in a directory, but only the first 3 levels deep and export it to a textfile.
I have earlier used the command "dir [path] /AD /S /B >"All folders.txt" in a batchfile to get a complete list of all subfolders in a directory,
but now I need a way to get the same listing but only the first three levels down.
0
Comment
Question by:Stefan_Soderquist
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 250 total points
Comment Utility
Greetings, Stefan_Soderquist.

Something like this should do it.

Dim objFSO, objTS, strOutputFile, varDepth

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the name and path of the output file as desired
Set objTS = objFSO.CreateTextFile(".\FileList.Txt")
varDepth = 0
'Change the starting path as desired
GetFolderNames "C:\"
objTS.Close
Set objTS = Nothing
Set objFSO = Nothing
Wscript.Echo "All done!"
Wscript.Quit

Private Sub GetFolderNames(strBaseFolder)
    Dim objSubFolders, objFolder, objSubFolder
    varDepth = varDepth + 1
    Set objFolder = objFSO.GetFolder(strBaseFolder)
    Set objSubFolders = objFolder.SubFolders
    GetFileNames objFolder, strBaseFolder & "\*.*"
    If varDepth <= 3 Then
        If objSubFolders.Count > 0 Then
            For Each objSubFolder In objSubFolders
                GetFolderNames objSubFolder
            Next
        End If
    End If
    Set objFolder = Nothing
    Set objSubFolder = Nothing
    Set objSubFolders = Nothing
    varDepth = varDepth - 1
End Sub

Private Sub GetFileNames(objFolder, strFileSpec)
    Dim objFiles, objFile
    Set objFiles = objFolder.Files
    If objFiles.Count > 0 Then
        objTS.WriteLine "Folder: " & objFolder.Path
        For Each objFile In objFiles
            objTS.WriteLine "    " & objFile.Name
        Next
    End If
    Set objFile = Nothing
    Set objFiles = Nothing
End Sub



Cheers!
0
 

Author Comment

by:Stefan_Soderquist
Comment Utility
Hi BlueDevilFan!

I only needed the folders, but it was easy to change (removed line 39-41)

Thanks for the script and the fast response. Job well done!
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Thanks, and you're welcome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now