Improve company productivity with a Business Account.Sign Up


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

Posted on 2015-02-16
Medium Priority
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  
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  
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  
Question by:acunaara
LVL 28

Expert Comment

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
        strTemp = strTemp & oFile.ReadLine & VbCrlf
    End If


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

Open in new window

LVL 61

Accepted Solution

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


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)
  WScript.Echo "No filename specified."
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
  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
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)
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing

Open in new window


Author Closing Comment

ID: 40613553
Excellent!! Thanks

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Go through the video which explain the procedure to export Amazon WorkMail single or multiple mailboxes to Outlook with Kernel Export Amazon WorkMail to PST tool. User can see the simple steps to export. It has salient features like filters (Include…

589 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