Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
81 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 28

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 59

Accepted Solution

by:
Bill Prew earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

782 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