Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-28
4
Medium Priority
?
290 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 1200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

927 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