Solved

VBScript to list subfolders, but only 3 levels deep

Posted on 2006-11-08
3
1,103 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now