Jump to content

Better way to edit text config file?

Recommended Posts

I am attempting to change a value in a text config file. I have a script that reads line 9 of the file and replaces the text.  My concern is that on some machines the cfg may have more or less options so the setting I need to change may be on a different line.   Where I am having trouble is searching for "a4.station.name=something".  I know the first part of the string but the "something" is unknown.  Here an example of the config file:

# 6/26/2017 3:32:09 PM
a4.organization.name=Contoso Ltd
a4.station.name=Something I don't know

Here is my script:

#include <FileConstants.au3>
#include <File.au3>
$file = ("C:\somepath\a4.cfg")
$current = FileReadLine($file, 9)
$newname = InputBox($current, "New name for this station?")
_ReplaceStringInFile($file, $current, "a4.station.name=" & $newname)

I have tried iniwrite but the file doesn't have  section headers so it adds it to end of the file.   Any help would be appreciated.  Thanks in advance.

Share this post

Link to post
Share on other sites


Read the file into an array, search for the correct line and amend it, rewrite the file:

#include <Array.au3>

$sInsert = "Fred is a happy boy"

; Read in whole file into an array (simulated here)
Global $aLines[] =  ["#", _
    "# 6/26/2017 3:32:09 PM", _
    "a4.max.logins.timeout=20", _
    "a4.turn.port=67000", _
    "a4.pfx.password=something", _
    "a4.organization.name=Contoso Ltd", _
    "a4.http.port=80", _
    "a4.https.port=120", _
    "a4.station.name=Something I don't know", _
    "a4.video.minport=1234", _
    "a4.turn.maxport=1240", _
    "a4.audio.gain=20", _
    "a4.ice.type=local-turn", _
    "a4.turn.minport=53102", _
    "a4.tab.window.left=52131", _
    "a4.tab.window.right=78942", _

For $i = 0 To UBound($aLines) - 1
    If StringInStr($aLines[$i], "a4.station.name") Then
        $aLines[$i] = "a4.station.name=" & $sInsert

_ArrayDisplay($aLines, "", Default, 8)

; Now rewrite file from the array


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

Melba beat me to it but I'll post my example as well :)

#include <Array.au3>
#include <File.au3>
Global $aConfig, $sConfig = @ScriptDir & "\Config.cfg"
_FileReadToArray($sConfig, $aConfig)
For $i = 1 To $aConfig[0]
    If StringInStr($aConfig[$i], "a4.station.name=") Then $aConfig[$i] = "a4.station.name=SomethingNew"
_FileWriteFromArray($sConfig, $aConfig, 1)



Share this post

Link to post
Share on other sites

Thank you both very much!  I fell much better running the script now that it looks for the value instead of blindly replacing a line.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...