The following script shows how to use the FileSystemObject-object with recursive procedure calls to list all folders and subfolder-names of a given folder.
'************************************************
' File: WSHRecurseFolders.vbs (WSH-sample in VBScript)
' Author: (c) Günter Born
'
' Lists all subfolders of given folder using
' the FileSystemObject. Implements a recursive
' search, to get all subfolders within a directory-tree.
'
' In no way shall the author be liable for any
' losses or damages resulting from the use of this
' program. Use AS-IS at your own risk.
'
' The code is property of the author. You may
' use the code and modify it, as far as this header
' remains intact. Further updates and other samples
' may be found on my site:
' http://www.Borncity.de
'************************************************
Option Explicit
Const pfad = "%windir%\System" ' Testpath
'Const pfad = "%windir%\All Users"
Dim Text, Title, index, Txt()
Dim fso, wsh, i ' Object variable
Text = "Folder " + vbCRLF + vbCRLF
Title = "WSH sample - by Günter Born"
index = 1
Set wsh = WScript.CreateObject ("WScript.Shell")
' Create a new FileSystemObject-Object, which we need
' for file system access.
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get the folders collection
RecFolder index, wsh.ExpandEnvironmentStrings(pfad)
' Get the result (into Txt(i))
For i = 1 to Ubound(Txt)
Text = Text + Txt(i) + vbCRLF
Next
' Show result in a dialog
MsgBox Text, vbOkonly + vbInformation, Title
Function RecFolder (idx, pfad)
' Recursive folder handling
Dim fo, fc, i
' get Folders-collection
Set fo = fso.GetFolder(pfad)
Set fc = fo.SubFolders
Redim Preserve Txt(idx) ' redim Text-field
For Each i in fc ' get all folders
Txt(idx) = Txt(idx) + pfad + "\" + i.name + vbCRLF
' search for subfolders
Call RecFolder (idx+1, pfad + "\" + i.name)
Next
Set fo = Nothing ' release objects
Set fc = Nothing
End function
' End
|
Additional information about this sample and how to access the FileSystemObject object may be found in my WSH books.