Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to modify a VBScript that edits an XML file

Posted on 2011-05-04
4
Medium Priority
?
533 Views
Last Modified: 2012-05-11
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.
<comInterfaceExternalProxyStub
     iid="{RaysText}"
     name="_IGenericWbEvents"
     tlbid="{680E3C06-96E8-4127-902A-1B5100DC14C1}"
     proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"/>


Second:Here are the same lines, but after the VBScript has been run.
<!--<comInterfaceExternalProxyStub
     iid="{RaysText}"
     name="_IGenericWbEvents"
     tlbid="{680E3C06-96E8-4127-902A-1B5100DC14C1}"
     proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"/>-->
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)
objFile.Close

' 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) & "-->"
    End If
Next
' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
objFile.Write(Join(arrLines, vbCrLf))
objFile.Close :


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.





0
Comment
  • 3
4 Comments
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35690022

' 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)
objFile.Close

mylist = "RaysText,BobsText"
ArrayOfValues = Split(mylist,",")
For i = 0 To UBound(ArrayOfValues)
	' Loop through all lines of the file
	For i = 0 To UBound(arrLines)-1
    	' Look for pattern to match
    	intOffset = Instr(arrLines(i), ArrayOfValues(i))
    	if intOffset > 0 Then
        	arrLines(i-1) = "<!--" & arrLines(i-1)
        	arrLines(i+3) = arrLines(i+3) & "-->"
    	End If
	Next
Next
' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
objFile.Write(Join(arrLines, vbCrLf))
objFile.Close :

Open in new window

0
 
LVL 6

Accepted Solution

by:
cfEngineers earned 2000 total points
ID: 35690031
Sorry had duplicate "i"'s
' 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)
objFile.Close

mylist = "RaysText,BobsText"
ArrayOfValues = Split(mylist,",")
For x = 0 To UBound(ArrayOfValues)
	' Loop through all lines of the file
	For i = 0 To UBound(arrLines)-1
    	' Look for pattern to match
    	intOffset = Instr(arrLines(i), ArrayOfValues(x))
    	if intOffset > 0 Then
        	arrLines(i-1) = "<!--" & arrLines(i-1)
        	arrLines(i+3) = arrLines(i+3) & "-->"
    	End If
	Next
Next
' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)
objFile.Write(Join(arrLines, vbCrLf))
objFile.Close :

Open in new window

0
 
LVL 1

Author Closing Comment

by:Accidental Hyper-V Administrator
ID: 35690819
Perfect !
Absolutely perfect !

I can't thank you enough.
0
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35691000
Any time
also you can get rid of line 9 i forgot to delete that.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question