Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

9 Experts available now in Live!

Get 1:1 Help Now