Solved

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

Posted on 2013-10-29
6
473 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now