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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to replace ip address in an ini file 1

Status
Not open for further replies.

lunasfirebird

Programmer
Jun 3, 2004
2
US
If someone could help me with this I would appreciate it. I need to change a line in a xxx.ini file from Host=xxx.xxx.xxx.xxx to Host=yyy.yyy.yyy.yyy. I know the name of the file and where it is, and plan on having the user run the script to change the IP address for our new dialup IP.

I've looked and searched thru everything I could find and will continue after this posts, but most of the solutions seem really complex and I am really new at vbscripts.

 
What have you tried thus far?

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
I'm still trying to find out the right command set to use. Right now it looks like I have to read a record from one file and write it to a new file with the changes. That doesn't seem right and the vbscript help file isn't much help.
 
Sounds like you have the right idea. Here is some sample code that might help:
Code:
Option Explicit

Dim oFSO
Dim strLine
Dim oOldFile
Dim oNewFile
Dim i

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oOldFile = oFSO.OpenTextFile("myFile.txt", 8)
Set oNewFile = oFSO.CreateTextFile("myTemp.txt")

i = 1
While oOldFile.AtEndOfStream <> True
  strLine = oOldFile.ReadLine()
  'Do whatever changes you want here
  'In this eample I will number the lines
  strLine = i & ") " & strLine
  oNewFile.WriteLine strLine
  i = i + 1
Wend

oOldFile.Close
oNewFile.Close
Set oOldFile = Nothing
Set oNewFile = Nothing

'Delete the old file and copy in the new one
oFSO.DeleteFile "myFile.txt"
oFSO.CopyFile "myTemp.txt", "myFile.txt"
oFSO.DeleteFile "myTemp.txt"

Set oFSO = Nothing
[code]

I did this off the top of my head, so it is untested. Also you'd want to put in some error handling in the final script.
 


[blue][i]"Well, once again my friend, we find that science is a two headed beast.  One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/i][/blue]
 
the code above is good if you want to replace the file, but if it has any customizations for your user you will need to actually edit it. This will do the trick.

Code:
Const ForReading = 1
Const ForWriting = 2
Const ReadOnly = 1
Dim path

Set fso = CreateObject("Scripting.FileSystemObject")
'Find the iniPath
iniPath = "C:\Windows\"

Set fsoFile = fso.GetFile(iniPath & "Module.ini")

bToggledReadOnlyAttribute = False
If fsoFile.Attributes And ReadOnly Then
	fsoFile.Attributes = fsoFile.Attributes - ReadOnly
	bToggledReadOnlyAttribute = True
End If


Set fsoTextStream = fso.OpenTextFile(iniPath & "Module.ini", ForReading)
strOldBootIni = fsoTextStream.ReadAll
fsoTextStream.Close

'HERE IS WHERE WE SET THE OLD AND NEW VALUES TO REPLACE
strNewBootIni = Replace(strOldBootIni, "COMMPOLLSIZE=On", "COMMPOLLSIZE=Off")


Set fsoTextStream = fso.OpenTextFile(iniPath & "Module.ini", ForWriting)
fsoTextStream.Write strNewBootIni
fsoTextStream.Close

If bToggledReadOnlyAttribute = True Then
	fsoFile.Attributes = fsoFile.Attributes + ReadOnly
End If
End If

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
the vbscript help file isn't much help
Take a look at FileSystemObject (merely all methods, properties and See also topics are of interest), InStr, Mid.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oops, please delete the last End If from my script sample.

I grabbed this code from another of my scripts that was doing some other checking before editing the file.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top