Solved

How to open a file, look for a line and replace/add text via a script?

Posted on 2008-10-28
4
282 Views
Last Modified: 2012-05-05
How would you recemend that I could open a file (Test1.log) and search for a line of text then pass data to that line via a script?  I have attached the file and the line I would like to edit.  I would like to be able to pass 123456789 as the value between the "", then save it (ex "123456789" />)
Before
<add key="websvc.PAD.ip"                      value= "" />

After
<add key="websvc.PAD.ip"                      value= "123456789" />

Please help...
Test1.log
0
Comment
Question by:John_2000
[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
  • 2
  • 2
4 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22823955
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFile variable with the location of the log file.  Running the script will replace any instances of the original string with the new one.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strFile = "c:\test1.log"
strOldString = "<add key=" & Chr(34) & "websvc.PAD.ip" & Chr(34) & "	    		value= " & Chr(34) & Chr(34) & " />"
strNewString = "<add key=" & Chr(34) & "websvc.PAD.ip" & Chr(34) & "	    		value= " & Chr(34) & "123456789" & Chr(34) & " />"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
 
strText = objTextFile.ReadAll
strText = Replace(strText, strOldString, strNewString)
objTextFile.Close
 
Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
 
objTextFile.Write strText
objTextFile.Close

Open in new window

0
 

Author Comment

by:John_2000
ID: 22825441
That works great!  But one more thing.....If there was a value already between the quotes, it will not update.  Would it be easy adjust this code to reflect that?
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 300 total points
ID: 22825883
This version will replace any line containing <add key="websvc.PAD.ip" with the new line.

It was adapted from http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov04/hey1130.mspx


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strFile = "c:\test1.log"
strOldString = "<add key=" & Chr(34) & "websvc.PAD.ip" & Chr(34)
strNewString = vbTab & vbTab & "<add key=" & Chr(34) & "websvc.PAD.ip" & Chr(34) & "                    value= " & Chr(34) & "123456789" & Chr(34) & " />"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
 
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
 
    intLineFinder = InStr(strNextLine, strOldString)
    If intLineFinder <> 0 Then
        strNextLine = strNewString
    End If
 
    strNewFile = strNewFile & strNextLine & vbCrLf
Loop
 
objTextFile.Close
 
Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
 
objTextFile.WriteLine strNewFile
objTextFile.Close

Open in new window

0
 

Author Closing Comment

by:John_2000
ID: 31510822
Nice Job!
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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