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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iterating through unknown number of subdirectories

Status
Not open for further replies.

dotolee

Technical User
Jan 27, 2008
134
CA
hi there. i have to write some script that will accept a path and then search through that path and all subdirectories for specific file types.
So far, to test my logic, I just assumed that we'll only have 2 levels of subdirectories. the pseudocode looks like:
set fso = file system object
strpathToCheck= topLevelPath (eg. c:\mydirectory> )

ofiles = fso.getfiles(strPathToCheck)
for each file in ofiles
check file type. if it's asp, process it.
next
if fso.hasfolders(strPathtoCheck) then
ofolders = fso.getfolders(strPathToCheck)
for each subfolder in ofolders
check each file
next
End if

The problem is that each subfolder can have multiple subfolders. can you suggest how i can write a reuseable function that it will search through all subfolders and return with a list of files that need to be checked, or will check each one as it finds them?
Thanks.
 
Code:
CONST strDir = "C:\temp\"
CONST strExt = ".txt"

set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

function searchExt(strDir, strExt)
   set objFolder = objFSO.GetFolder(strDir)
   for each objSubFolder in objFolder.SubFolders
      strResults = searchExt(objSubFolder.Path, strExt)
   next
	
   for each objFile in objFolder.Files
      if (right(objFile.Path, len(strExt)) = strExt) then strResults = strResults & objFile.Path & vbNewLine
   next
   searchExt = strResults
end function

strFiles = searchExt(strDir, strExt)

if you next the results in an array, simply split the string

Code:
arrFiles = split(strFiles, vbNewLine)

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top