mithandir1 Posted June 27, 2017 Share Posted June 27, 2017 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.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 a4.max.logins=2 Here is my script: #include <FileConstants.au3> #include <File.au3> $file = ("C:\somepath\a4.cfg") Fileopen($file) $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. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 27, 2017 Moderators Share Posted June 27, 2017 mithandir1, 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", _ "a4.max.logins=2"] For $i = 0 To UBound($aLines) - 1 If StringInStr($aLines[$i], "a4.station.name") Then $aLines[$i] = "a4.station.name=" & $sInsert ExitLoop EndIf Next _ArrayDisplay($aLines, "", Default, 8) ; Now rewrite file from the array M23 mithandir1 1 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Subz Posted June 27, 2017 Share Posted June 27, 2017 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" Next _FileWriteFromArray($sConfig, $aConfig, 1) _ArrayDisplay($aConfig) mithandir1 1 Link to comment Share on other sites More sharing options...
mithandir1 Posted June 27, 2017 Author Share Posted June 27, 2017 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now