VB Script to replace text in an existing INI file

Posted on 2009-04-22
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
    LVL 39

    Assisted Solution

    Apparently, that's not trivial. Luckily somebody else has done that job for you already. Try this one: with your script.

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

    Expert Comment

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

    Accepted Solution

    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, "", "TIMEPIECE ")
    Set objFile = objFSO.OpenTextFile("C:\Program Files\General.ini", ForWriting)
    objFile.WriteLine strNewText

    Open in new window

    LVL 39

    Expert Comment

    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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now