Avatar of tonydemarco
tonydemarco
Flag for United States of America asked on

strTargetFile= VBS Recurse process in folder

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

Open in new window

VB Script

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tonydemarco

ASKER
You are right. I'm working too many hours.
I will however post another question tomorrow as soon as I get the batch code written that will attempt to take the parsed results and transpose the data from line to column.

Thanks for your help Bill
Bill Prew

Welcome, get some rest :-).

~bp
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck