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

x
?
Solved

VB Script to replace text in an existing INI file

Posted on 2009-04-22
9
Medium Priority
?
647 Views
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.
0
Comment
Question by:hsbgit
  • 4
6 Comments
 
LVL 39

Assisted Solution

by:abel
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.
0
 
LVL 9

Expert Comment

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

Accepted Solution

by:
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
objFile.Close
strNewText = Replace(strText, "DC1.sales.contoso.com", "TIMEPIECE ")
 
Set objFile = objFSO.OpenTextFile("C:\Program Files\General.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

by:abel
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.
0
 
LVL 39

Expert Comment

by:abel
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?
0
 
LVL 39

Expert Comment

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

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