Solved

VBScript to list subfolders, but only 3 levels deep

Posted on 2006-11-08
3
1,122 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
ID: 17897010
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
ID: 17897112
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
ID: 17897132
Thanks, and you're welcome.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

792 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