troubleshooting Question

strTargetFile= VBS Recurse process in folder

Avatar of tonydemarco
tonydemarcoFlag for United States of America asked on
VB Script
3 Comments1 Solution665 ViewsLast Modified:
I have attached the following code contained in a vbs file that I use to parse a file.
I would like to modify this code so that it will process every file in the folder.
Each file has the same prefix "h837.2010".

Thanks
Dim sCurPath
CurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

Const ForReading = 1
Const ForWriting = 2

strFolderName = CurPath
strTargetFile = CurPath & "\837a.txt"
strFileNameConstant = "h837.2010"	
strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent")

arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
    strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"
 
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles
    
    If InStr(Lcase(objFile.Name),LCase(strFileNameConstant)) Then
    
		Set objFile = objFSO.OpenTextFile(objFile.Name, 1)
		strSearchString = objFile.ReadAll
		objFile.Close
		Set objRegEx = CreateObject("VBScript.RegExp")
		objRegEx.Global = True  
		objRegEx.Pattern = "BHT"
		strNewText = objRegEx.Replace(strSearchString,VBCrlf & "BHT" & VBCrLf) 

    End If
Next

'Writes Target file
Set objFile = objFSO.OpenTextFile(strTargetFile, ForWriting, true)
objFile.WriteLine(strNewText)
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("837a.txt", 1) 
strSearchString = objFile.ReadAll 
objFile.Close 
Set objRegEx = CreateObject("VBScript.RegExp") 
objRegEx.Global = True   
objRegEx.Pattern = "~" 
strNewText = objRegEx.Replace(strSearchString, VBCrLf) 

Set objOUtFile = objFSO.CreateTextFile("837b.txt", 2) 
objOutFile.WriteLine strNewText 
objOutFile.Close
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros