Re: Folder enumeration



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?


.



Relevant Pages

  • Re: File name changes on opening - adds a number to end
    ... I have amended the VBS as per your last message but I still get the error ... > 'get the path to the temp folder ... > sub SelectFiles ... >> message is not refering to the actual opening of the script but something the ...
    (microsoft.public.excel.misc)
  • Re: VBScript to copy shortcuts
    ... ' In this script a shortcut 'shortcut.lnk' is copied to the desktop ... ' An example of how to delete multiple files of the same type in a folder. ... ' Paul Williams, msresource.net, February 2005. ... ' Sub routine simply echos the passed string. ...
    (microsoft.public.win2000.active_directory)
  • Re: User account and Personal folder mismatch script
    ... I believe you want a script that can be run by an administrator that will ... The script above only documents if a home folder has been assigned. ... > End Sub ...
    (microsoft.public.scripting.vbscript)
  • Re: One for the NTFS permissions gurus.
    ... user logs-on the script creates a new subfolder based on the username. ... What I want to do is to set the permissions on the parent folder (the shared ... but other users have no rights. ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Verifying a user without knowing his precise OU
    ... line as "CScript orphan.vbs" After inputting the server name and location ... folders exist, who owns them with the full OU or that the folder is bad. ... This is a script to check for orphaned home folders. ... 'Sub routine for preparing the output log file. ...
    (microsoft.public.scripting.vbscript)