Solved

Need VBScript to delete all lines between two comment lines inside an XML file.

Posted on 2013-10-29
6
1,115 Views
Last Modified: 2013-10-29
I have an XML file that has a number of lines that need to be deleted.

The lines that need to be deleted, are sandwiched between the following two lines.
<!-- ======= Start of lines to be deleted =======-->
<!-- ======= End of lines to be deleted =======-->

There could be any number of lines between the above two comment lines.

And, the comment lines need to remain after the deletes are completed.

Can I ask for some help in writing this ?

Thank you so very much in advance.
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39609484
Can you provide a sample of the actual XML file that needs to be changed, for testing?

~bp
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39609654
This seems to work in a test set of data for me.  Save as a VBS file, and run as follows from a command line or BAT file.

cscript EE28280241.vbs in.xml out.xml

or

cscript EE28280241.vbs in.xml

In the first case it will read in.xml, and make the changes, and save the result to out.xml.  In the second case it will read in.xml, make the changes and overwrite in.xml with the changed version.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define start and end of comment strings
strCommentStart = "<!-- ======= Start of lines to be deleted =======-->"
strCommentEnd = "<!-- ======= End of lines to be deleted =======-->"

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Replace desired strings
sData = DoReplace(sData)

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing

' Remove all characters between comment start and end strings
Function DoReplace(sData)
   blnDone = False
   DoReplace = sData
   intOffset = 1

   ' Loop until no more comments to process
   Do While Not blnDone
      intStartLeft = Instr(intOffset, DoReplace, strCommentStart, vbTextCompare)
      If intStartLeft > 0 Then
         intStartRight = intStartLeft + Len(strCommentStart) - 1
         intEndLeft = Instr(intStartRight, DoReplace, strCommentEnd, vbTextCompare)
         If intEndLeft > 0 Then
            intEndRight = intEndLeft + Len(strCommentEnd) - 1
            DoReplace = Left(DoReplace, intStartRight) & vbCrLf & Mid(DoReplace, intEndLeft)
            intOffset = intEndRight - (intEndLeft - intStartRight - 1) + 2
         Else
            blnDone = True
         End If
      Else
         blnDone = True
      End If
   Loop
End Function

Open in new window

~bp
0
 
LVL 1
ID: 39609670
I'll try it right away.

Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1
ID: 39609730
Magnificent !
Phenomenal!

I can't thank you enough for this.


There is a second part to this that I was afraid to ask for the first "go round".

I'm going to close this.
And open a new incident and once again beg for your much appreciated help.
0
 
LVL 1

Author Closing Comment

by:Accidental Hyper-V Administrator
ID: 39609742
This works perfectly.

Your the greatest Bill !
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39609908
Very welcome, thanks for the feedback.

~bp
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

632 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