Solved

How to replace each line of a config file that contain the keyword with a unique string using VBS?

Posted on 2015-02-16
3
55 Views
Last Modified: 2015-02-16
I have the below configuration file and i want to replace each line that contain the keyword "REPEAT" with the below content: "REPEAT" "_Total" 60 MAJOR 95 * 0000 2400

Config file:
DURATION             = 1  
AUTOMATIC_TRIAL = NO  
VIRTUAL_SERVER_FLAG      = NO  
DISPLAY_TYPE            = LONG
DISPLAY_WIDTH            = 80
"REPEAT"  "_Total" 60 MAJOR 95 *     0000 2400
"REPEAT"       "_Total" 60        MAJOR 95 5 0000      2300
"REPEAT"     "_Total" 60       MAJOR 70 8 0150            2890
"REPEAT" "_Total"   60       MINOR 95 * 0880 2400
"REPEAT"        "_Total"    60 MAJOR 99 * 1100 2400
RESTART             = 7  


So the output should be:

DURATION             = 1  
AUTOMATIC_TRIAL = NO  
VIRTUAL_SERVER_FLAG      = NO  
DISPLAY_TYPE            = LONG
DISPLAY_WIDTH            = 80
"REPEAT" "_Total" 60 MAJOR 95 * 0000 2400
"REPEAT" "_Total" 60 MAJOR 95 * 0000 2400
"REPEAT" "_Total" 60 MAJOR 95 * 0000 2400
"REPEAT" "_Total" 60 MAJOR 95 * 0000 2400
"REPEAT" "_Total" 60 MAJOR 95 * 0000 2400
RESTART             = 7  
0
Comment
Question by:acunaara
3 Comments
 
LVL 26

Expert Comment

by:MacroShadow
ID: 40612796
Try this:
strFileName = "C:\Temp\OriginalTextFile.txt"

Set oFso = CreateObject("Scripting.FileSystemObject")
Set oFile = oFso.OpenTextFile(strFileName)

Do Until oFile.AtEndOfStream
    If Left(oFile.ReadLine, 8) = Chr(34) & "REPEAT" & Chr(34) Then
        strTemp = strTemp & Replace(oFile.ReadLine, """REPEAT""", """REPEAT"" " """_Total """ "60 MAJOR 95 * 0000 2400")) & VbCrlf
    Else
        strTemp = strTemp & oFile.ReadLine & VbCrlf
    End If
Loop

oFile.Close

oFile = "C:Temp\StrippedTextFile.txt"
Set Set oFile = objFSO.CreateTextFile(oFile, True)
objFile.Write strTemp
objFile.Close

Open in new window

0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40612911
Here is an approach that uses a regular expression to find the lines to replace.  Save as a VBS, and then run one of two ways from the command line (or batch script):

cscript EE28617909.vbs in.txt out.txt

or

cscript EE28617909.vbs in.txt

The first method will read from the first file and write to the second with any updates.  The second approach will read the file, update it, and then overwrite the same file with the changed version.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' 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
Set oRegExp = New RegExp
oRegExp.Global = True
oRegExp.IgnoreCase = False
oRegExp.Pattern = "(""REPEAT"").*"
sData = oRegExp.Replace(sData, "$1 ""_Total"" 60 MAJOR 95 * 0000 2400")

' 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
Wscript.Quit

Open in new window

~bp
0
 

Author Closing Comment

by:acunaara
ID: 40613553
Excellent!! Thanks
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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

19 Experts available now in Live!

Get 1:1 Help Now