Solved

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

Posted on 2013-10-29
6
470 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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Oops, someone beat me to it...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Closing Comment

by:Accidental Hyper-V Administrator
Comment Utility
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

Author Comment

by:Accidental Hyper-V Administrator
Comment Utility
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 51

Expert Comment

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

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

18 Experts available now in Live!

Get 1:1 Help Now