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
60 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 27

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 53

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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