replacing values in .ini file with %computername%
Hi
i need a script of some decription to replace txt in an .ini file with a variable as below
i need to replace a value in an .ini file with the %computername% variable
i.e i have an ini at c:\heyex\heyex.ini" and i need to replace the workstation=installation value with workstation=%computername%
i have the following script
Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim blnRes: blnRes = 0
Dim strOld, strNew
strOld = "installation"
strNew = "%computername%"
On Error Resume Next
Call update("C:\heyex\heyex.ini", strOld, strNew)
blnRes = blnRes Or (Err.Number = 0): Err.Clear
Call update("C:\heyex.ini", strOld, strNew)
blnRes = blnRes Or (Err.Number = 0): Err.Clear
On Error GoTo 0
If blnRes Then MsgBox("Success!") Else MsgBox("Fail!") End If
Sub update(strFile, strOld, strNew)
Dim objFSO, objFile, strText, strNewText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOld, strNew)
Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close
End Sub
but that just puts in %computername% and not the variable from the machine
can anyone advise what to add?
thanks
Answers (1)
http://www.robvanderwoude.com/vbstech_network_names_computer.php
You need to query the system as VBS wont outright know the varible.
So use bit of code to query for the computer name, and then assign that info to your varible strNew.
Option Explicit
Dim WshShell
Set wshShell = CreateObject("WScript.Shell")
Const ForReading = 1
Const ForWriting = 2
Dim blnRes: blnRes = 0
Dim strOld, strNew
strOld = "installation"
strNew = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set wshShell = Nothing
On Error Resume Next
Call update("C:\heyex\heyex.ini", strOld, strNew)
blnRes = blnRes Or (Err.Number = 0): Err.Clear
Call update("C:\heyex.ini", strOld, strNew)
blnRes = blnRes Or (Err.Number = 0): Err.Clear
On Error GoTo 0
If blnRes Then MsgBox("Success!") Else MsgBox("Fail!") End If
Sub update(strFile, strOld, strNew)
Dim objFSO, objFile, strText, strNewText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOld, strNew)
Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close
End Sub
Comments:
-
how do i add that to what i have already got, sorry my scripting is limited and proving difficuilt to attain what i need. - keane2012 11 years ago
-
Haha! im not very good either, you need VSCab for this kinda stuff (:
But have a look at the above, should work...I hope. - rileyz 11 years ago -
doesnt work sadly errors :( - keane2012 11 years ago
-
Whoops! forgot to dim wshShell. See new text just under Option Explicit - rileyz 11 years ago