Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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…

632 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