Need to modify a VBScript that edits an XML file
Posted on 2011-05-04
Good morng all.
A while back, (1-27-11) I posted a request for a VBScript to modify an XML file.
And, the script I recieved from "billprew" was very helpful indeed.
We use it all the time.
I was wondering if I can ask for a bit of help with modifying this script.
First:Here's an example of what the the lines (in the XML file) look like before the VBSCript is run.
Second:Here are the same lines, but after the VBScript has been run.
As you can see, "comment" characters have been added at the begining and end of the block.
And here is the VBScript itself:' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
' Define file to process, and pattern to match
strFile = "C:\BETASys\BETASys.manifest"
strMatch = "RaysText"
' Read file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
arrLines = Split(objFile.ReadAll, VbCrLf)
' Loop through all lines of the file
For i = 0 To UBound(arrLines)-1
' Look for pattern to match
intOffset = Instr(arrLines(i), strMatch)
if intOffset > 0 Then
arrLines(i-1) = "<!--" & arrLines(i-1)
arrLines(i+3) = arrLines(i+3) & "-->"
' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
Now here is what I was looking for:In the above script, we look for a line that contains "RaysText".
And if found, the script adds the "comment" caracters.
As it turns out, there could very well be a "BobsText" as well as a "JimsText" and "MarysText" in addition to a "RaysText".
What I have done currently is to replicate the above code, for each of the additional "Text" lines that need to be scanned for.
IE: I copied the code that looks for "RaysText" and substitued "Bobstext".
You ge the idea.
I'm far from an experts with these things, but it DOES work.
I was wondering if there was a "classier" way to handle looking for the "Text" line values.
Rather than replicating the code for each iteration. "BobsText", "JimsText" and "MarysText"
LIke maybe, some variable could contain "BobsText", "JimsText" and "MarysText".
And then, the VBScript would look for any vaule contained within the variable when scanning the XML file.
And if ithe text IS found in the .XML file, then te comment lines are added, just like it does now.
My goal is to be able to go to one place in the VBScriopt, and modify a variable to either add or remove things to scan the XML file for.
Rather than needing to replicate lines of code or delete lines of code based on what I want to scan for.
I want to, kinda, sorta, make it easy for someone else to modify this VBScript as our needs change.
I hope the above makes sense.
Once again, I DO appreciate your time and help with this.
Thanks in advance.