Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1146
  • Last Modified:

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

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
Accidental Hyper-V Administrator
Asked:
Accidental Hyper-V Administrator
  • 3
  • 3
1 Solution
 
Bill PrewCommented:
Can you provide a sample of the actual XML file that needs to be changed, for testing?

~bp
0
 
Bill PrewCommented:
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
 
Accidental Hyper-V AdministratorAuthor Commented:
I'll try it right away.

Thanks.
0
Technology Partners: 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!

 
Accidental Hyper-V AdministratorAuthor Commented:
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
 
Accidental Hyper-V AdministratorAuthor Commented:
This works perfectly.

Your the greatest Bill !
0
 
Bill PrewCommented:
Very welcome, thanks for the feedback.

~bp
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now