Thank you.
'===========================================================
' DemoRecursiveFileSearch.vbs
'
' Demonstrates recursive file system search. This
' example searches the folder tree defined by the
' RootPath variable.
'
' In this example, RootPath = "C:\Program Files"
'
' SearchFolders takes a folder object argument and
' walks the Files collection of the folder, passing
' each file object to a SelectFile routine and then
' walks the SubFolders collection of the folder,
' calling itself recursively for each subfolder.
'
' The SelectFile routine applies whatever selection
' criteria is appropriate and adds the file object
' to a global SelectedFiles collection (dictionary).
'
' In this example, files with an underscore (_) in the
' file name are selected.
'
' On return to the mainline script, the SelectedFiles
' collection is walked and each selected file is
' passed to the ProcessFile routine.
'
' In this example, the full path of each file is
' simply echoed to the console window.
'
'===========================================================
' To adapt this example...
'
' * change the RootPath variable value
' * change the SelectFile selection logic
' * change the ProcessFile processing logic
'
'===========================================================
'
'================================== begin mainline
'
Option Explicit
Dim RootPath
RootPath = "C:\Program Files"
Dim SelectedFiles
Set SelectedFiles = CreateObject("Scripting.Dictionary")
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim RootFolder
Set RootFolder = fso.GetFolder(RootPath)
Call SearchFolders(RootFolder)
WScript.Echo SelectedFiles.Count & " files selected..."
Dim SelectedFile
For Each SelectedFile In SelectedFiles.Items
Call ProcessFile(SelectedFile)
Next
WScript.Echo "Done..."
WScript.Quit
'
'================================== End mainline
'================================== Begin procedures
'
'================================== ProcessFile
'
Sub ProcessFile(argFile)
WScript.Echo argFile.Path
End Sub
'================================== SelectFile
'
Sub SelectFile(argFile)
If InStr(argFile.Name, "_") Then
Set SelectedFiles(argFile.Path) = argFile
End If
End Sub
'================================== SearchFolders
'
Sub SearchFolders(argFolder)
Dim file, subfolder
For Each file In argFolder.Files
Call SelectFile(file)
Next
For Each subfolder In argFolder.SubFolders
Call SearchFolders(subfolder)
Next
End Sub
'
'================================== End procedures
--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US