Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing mulitple text strings in one go 1

Status
Not open for further replies.

tourcd

IS-IT--Management
Dec 5, 2005
37
Hi,

I have a text file called "Params.txt", it contains the following...

start
Servername =
IP Address =
end

I'm trying to append a server name and IP address on to the corresponding lines. I've got the following code that will happily append the server name but I can't figure out how I can append the IP address within in a neat way. I can open the file, write the line, close the file and repeat this for each parameter. I wanted a smarter way of doing it, can someone give me a pointer?

Many thanks

Here's an extract of my code...

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Params.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewServername = Replace(strText, "Servername = ", "Servername = " & ServerName.Value)

Set objFile = objFSO.OpenTextFile("C:\Params.txt", ForWriting)
objFile.WriteLine strNewServername
objFile.Close
 
Code:
...
strText = objFile.ReadAll
objFile.Close
strText = Replace(strText, "Servername = ", "Servername = " & ServerName.Value)
strText = Replace(strText, "IP Address = ", "IP Address = " & [i]IP_Address_Here[/i])    
Set objFile = objFSO.OpenTextFile("C:\Params.txt", ForWriting)
objFile.WriteLine strText
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Params.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewServername = Replace(strText, "Servername = ", "Servername = " & ServerName.Value)
[red]strNewServername = Replace(strServerName, "IP Address = ", "IP Address = " & IPAddress.Value)[/red]
Set objFile = objFSO.OpenTextFile("C:\Params.txt", ForWriting)
objFile.WriteLine strNewServername
objFile.Close

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks! It was a bit of a noddy question (ashamed).

What if a servername already existed in the text? So I would have to delete everything after the "=" first?
 
If the servername already existed then I would suggest using a regular expression (regex) to do the replace.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top