Solved

VBScript to list subfolders, but only 3 levels deep

Posted on 2006-11-08
3
1,170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses
Course of the Month6 days, 13 hours left to enroll

623 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