Re: Folder enumeration
- From: "scriptnovice" <tsurf44@xxxxxxxxxxx>
- Date: 5 Jan 2007 00:54:36 -0800
Thanks very much mayayana, creating a variable to count the folder
depth was a good idea. I have tweaked your code a bit and included it
below for reference in case anyone has a similar query:
Dim iDepth
Set FSO = CreateObject("Scripting.FileSystemObject")
sPath = "C:\Windows\System"
iDepth = 4
Depth = 1
ShowSubFolders sPath,Depth
Sub ShowSubFolders(Folder, Depth)
If iDepth = Depth Then Exit Sub '-- quit when depth is reached.
'-- do recursion here ....
Set Folder = FSO.GetFolder(Folder)
Set colSubfolders = Folder.Subfolders
For Each subfolder In colSubfolders
WScript.Echo Depth & vbTab & subfolder
ShowSubFolders subfolder, Depth + 1 '-- call next loop:
Next
End Sub
mayayana wrote:
One simple method would be to use a global
variable to count your recursions. Something
like:
Dim iDepth '-- declare global variable.
sPath = "C:\Windows\System"
iDepth = 0 '-- reset variable counter.
ShowSubFolders sPath, 3
Sub ShowSubFolders(Folder, Depth)
if iDepth = Depth then exit sub '-- quit when depth is reached.
'-- do recursion here ....
iDepth = iDepth + 1 '-- add one to recursion counter.
ShowSubFolders(Folder, Depth) '-- call next loop:
End Sub
Hi
I am trying to write a script to enumerate folders that will allow me
to specify how far down the folder structure I want the script to go.
I have code that will alow me to enumerate all the subfolders of a
given folder e.g.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Scripts")
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
Wscript.Echo objSubfolder.path
Next
and I have code which will allow me to loop through all the subfolders
of an entire folder structure e.g.
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
Say for example my folder structure is as follows:
C:\Folder\A-SubFolder\A-SubFolder-1\A-SubFolder-2
C:\Folder\B-SubFolder\B-SubFolder-1\B-SubFolder-2
I want my script to loop through displaying the structure only as far
as A-SubFolder -1 and B-SubSFolder-1. I would like the output of my
script to look like the following:
C:\Folder
C:\Folder\A-SubFolder
C:\Folder\A-SubFolder\A-SubFolder-1
C:\Folder\B-SubFolder
C:\Folder\B-SubFolder\B-SubFolder-1
I want to be able to change how far down my folder structure the script
traverse before I run the script.
Can anyone help?
.
- References:
- Folder enumeration
- From: scriptnovice
- Re: Folder enumeration
- From: mayayana
- Folder enumeration
- Prev by Date: Re: Folder enumeration
- Next by Date: Re: calling common routines from other jobs?
- Previous by thread: Re: Folder enumeration
- Next by thread: How can a vbscript running on a client use automation to open Word?
- Index(es):
Relevant Pages
|