Solved

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

Posted on 2008-10-28
4
278 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now