?
Solved

Need to modify VBScript to add 5 lines after deleting a number of others

Posted on 2013-10-29
6
Medium Priority
?
482 Views
Last Modified: 2013-10-30
Bill Prew, was kind enough to author a VBScript for me, that deleted a number of line between two comment lines, in a .manifest file.
And it works perfectly.

Now, I need at add 5 lines, to the same .manifest file, in place of the lines that that were deleted.

VBScript that Bill authored looks like this:
' 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


The five lines that I need to add, look like this:

<dependency xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly asmv2:dependencyType="install" asmv2:codebase="BETASys/BETASys.manifest" asmv2:size="326200">
      <assemblyIdentity name="BETASys" version="1.0.0.7" type="win32" processorArchitecture="x86" />
    </dependentAssembly>
  </dependency>


I have uploaded the manifest file, that needs to have the lines deleted from it and the 5 lines added to it.

I would appreciate this very VERY much.
Rays.manifest
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39610022
I think this should do what you want.  Run it the same was as in the last question, with either two file names, or just one.

' 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 =======-->"
strInsert = "  <dependency xmlns=""urn:schemas-microsoft-com:asm.v1"">" & vbCrLf & _
            "    <dependentAssembly asmv2:dependencyType=""install"" asmv2:codebase=""BETASys/BETASys.manifest"" asmv2:size=""326200"">" & vbCrLf & _
            "      <assemblyIdentity name=""BETASys"" version=""1.0.0.7"" type=""win32"" processorArchitecture=""x86"" />" & vbCrLf & _
            "    </dependentAssembly>" & vbCrLf & _
            "  </dependency>" & vbCrLf

' 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 & strInsert & 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 7

Expert Comment

by:SterlingMcClung
ID: 39610024
It took a while to figure out this code, but it looks like it is going to be a simple process to change this line of code:
DoReplace = Left(DoReplace, intStartRight) & vbCrLf & Mid(DoReplace, intEndLeft)

Open in new window

to something else.  The quotes in the text you want to insert are going to make things fun to get properly formatted.  Give me a moment...
0
 
LVL 7

Expert Comment

by:SterlingMcClung
ID: 39610027
Oops, someone beat me to it...
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

Author Closing Comment

by:Accidental Hyper-V Administrator
ID: 39611625
Fabulous !
This may sound a bit "over the top", but the results of your efforts are far beyond what I thought we would be able to accompish.

You sir, are the greatest !
0
 
LVL 1
ID: 39611631
Thank you so very much for offering your time to help me with my issue.

How kewl is it, to be in a position to help folks who need it.

Thanks again.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39611651
Glad that was helpful, you're far too kind, but thanks for the nice feedback, I appreciate it.

~bp
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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

765 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