Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 57

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

661 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