tags:

views:

505

answers:

1

Ok, Ive got a vbscript that iterates through a directory and its subfolders to retrieve a list of files. Example here:

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Scripts")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        ShowSubFolders Subfolder
    Next
End Sub

Now this is great for getting an extensive list, but horrible on performance if there is a deep folder hierachy.

So my question is, is there a way to edit this part of the script so that it only iterates through a set number of levels of subfolders? Due to the depths of folder structures an ideal amount of levels to drill down into would be 3 levels.

+6  A: 

Give your recursive call an exit condition ala

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Scripts"), 3 

Sub ShowSubFolders(Folder, Depth)
    If Depth > 0 then
        For Each Subfolder in Folder.SubFolders
            Wscript.Echo Subfolder.Path
            ShowSubFolders Subfolder, Depth -1 
        Next
    End if
End Sub
cmsjr