[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


VB Script to replace text in an existing INI file

Posted on 2009-04-22
Medium Priority
Last Modified: 2013-12-26
I am attempting to replace text in an existing ini file then re-save the file. I will eventually be pushing this vbs script out via GPO to multiple users. I have attached a copy of the text/ini file I am working on. I would like to replace the server information on line 4.

Any help would be appreciated.
Question by:hsbgit
  • 4
LVL 39

Assisted Solution

abel earned 2000 total points
ID: 24210832
Apparently, that's not trivial. Luckily somebody else has done that job for you already. Try this one: http://www.robvanderwoude.com/vbstech_files_ini.php with your script.

PS: your attached file did not work, apparently, it is not visible.

Expert Comment

ID: 24210955
It may be simpler just to replace the whole file via batch file from GP.

Accepted Solution

hsbgit earned 0 total points
ID: 24211068
I actually found a script that I edited to work. All is well now:

Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Program Files\General.ini", ForReading)
strText = objFile.ReadAll
strNewText = Replace(strText, "DC1.sales.contoso.com", "TIMEPIECE ")
Set objFile = objFSO.OpenTextFile("C:\Program Files\General.ini", ForWriting)
objFile.WriteLine strNewText

Open in new window


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 39

Expert Comment

ID: 24211178
well, that's quite a quick&dirty way of doing it, but if it works for you, that's good :). Normally when you read/write INI files you go by section > key > value, which was what the code pointed at in my original post was about.
LVL 39

Expert Comment

ID: 24691828
I don't necessarily object to the closing but a split may be more in order here. The ReadINI, WriteINI and DeleteINI functions in my post make it possible to go through Section > Key > Value of an INI file, imho an important feat of INI file writing/reading. When there are duplicate values in different sections the posted code in hsbgit's comment above will not work. What about a split points?
LVL 39

Expert Comment

ID: 24709752
I suggest split, as suggested in my prev. comment: http:#24211068 and http:#24210832

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

873 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