Solved

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

Posted on 2013-10-29
6
476 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 53

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 53

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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