Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript to list subfolders, but only 3 levels deep

Posted on 2006-11-08
3
Medium Priority
?
1,211 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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

715 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