Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerate folders to a set depth.. 1

Status
Not open for further replies.

blakey2

MIS
Jan 28, 2004
313
AU
Hi All,

Perhaps this is somehing simple to do, however i can't seem to work it out ;-D

I want a script to enumerate sub folders to a depth of 2.

Plenty of scripts on the net which enumerate all sub-folders, however I don't want to enumerate all subfolders.

Eg:
Is there some sort of index/count property..? Worst case I think i'll have to enumerate everything, store it in an array and count the slashes to determine path depth.. just thought there might be a more elegant solution?

Any help/suggestions/reading appreciated.

Cheers.
 
What is your actual code ?
You may add a level parameter to your recursive function .

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I did this a while ago...it's been a while since I looked at it.

Code:
Option Explicit

RecurseXDeep "C:\Scripts", 2

Sub RecurseXDeep(strFolderPath, intDirDeep)
	Dim RegEx : Set RegEx = New RegExp
	RegEx.Pattern = Replace(strFolderPath, "\", "\\") & _
					"\\?([^\\]+\\?){" & intDirDeep & "}$"
	RegEx.IgnoreCase = True
	RegEx.Global = True
	Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
	Dim objDict : Set objDict = CreateObject("Scripting.Dictionary")
	
	RecurseFolder objFSO, objDict, strFolderPath, RegEx
	
	WScript.Echo Join(objDict.Keys, VbCrLf)
End Sub

Sub RecurseFolder(objFSO, objDict, strFolderPath, RegEx)
	Dim objFolder : Set objFolder = objFSO.GetFolder(strFolderPath)
	
	Dim objFile
	For Each objFile In objFolder.Files
		objDict.Add objFile.Path, ""
	Next
	
	Dim objSubFolder 
	For Each objSubFolder In objFolder.SubFolders
		If RegEx.Test(objSubFolder.Path) Then RecurseFolder objFSO, objDict, objSubFolder.Path, RegEx
	Next
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi PHV,

Thanks for the reply. I had taken this code from the hey scripting guy site:

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Scripts"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next
Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            Wscript.Echo objFile.Name
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

This enumerates all subfolders and I can't work out how to conditionally break the loop by using a std counter type approach.. I could paste in some attempts but they don't work..

Cheers
 
You may try this:
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Scripts"
[!]intMaxLevel = 2[/!]
Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next
Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)[!], 1[/!]

Sub ShowSubFolders(Folder[!], Level[/!])
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            Wscript.Echo objFile.Name
        Next
        Wscript.Echo
        [!]If Level < intMaxLevel Then[/!]
            ShowSubFolders Subfolder[!], Level+1[/!]
        [!]End If[/!]
    Next
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks both PHV and dm4ever for your responses.

Had a look at both scripts and cut a little out of PHV's to make it closer resemble what I am after.

(I got a little lost with the regex string from dm4ever's script)

Resultant script:

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "F:\test"
intMaxLevel = 2
Set objFolder = objFSO.GetFolder(objStartFolder)

ShowSubfolders objFSO.GetFolder(objStartFolder), 1

Sub ShowSubFolders(Folder, Level)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files

        If Level < intMaxLevel Then
            ShowSubFolders Subfolder, Level+1
        End If
    Next
End Sub

Script is almost perfect for my requirements and I should be able to add the additional logic/processing I require to get everything done.

Cheers, Chris!
 
Whatever works for you and you feel comfortable with. Good luck.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top