kabar Posted April 28, 2009 Share Posted April 28, 2009 I am a serious NOOB with scripting. So take it easy on me. I have written the below code to add a new section to an INI if it does not exist. It arrays the ini and tells me if the [section] already exists. The cheesy part is that I have it opening the INI so that it can be edited manually. I would like it to search for a existing [section] find the first available (empty line) after the existing [section] and append (incrementally as in the below ini) the "name and value" that the it will get from another source. then insert another empty space. The is part of my code in question: ;#cs********************************CODE************************************** #include <file.au3> #include <array.au3> $answer = "Section 2" Dim $logArray $AdminRights = "C:\Remote.ini" If FileExists($AdminRights) Then _FileReadToArray($AdminRights, $logArray) EndIf $result = _ArraySearch ( $logArray, $answer, 0, 0, 0, True) If $result < 0 Then Exit If $Result > 1 Then $open = MsgBox(4,"Open Remote.ini?", $answer & "* Already Exists. Do you want to open Remote.ini?") If $open = 7 then exit ;Endif Run(@comspec & ' /c start ' & $AdminRights, '', @SW_HIDE) EndIf Sleep(1000) ;WinWait("Remote.ini-Notepad") Send("^f") Sleep(1000) Send($answer) ;Sleep(1000) Send("!f") ;WinWaitClose("Remote.ini-Notepad") Exit ;#ce********************************END OF CODE************************************** Example of the INI I am working with. [section 1] ID1=LON\20298 [section 2] ID1=LON\526756 ID2=LON\7456 ID3=LON\722456 [section 3] ID1=LON\67437 [section 4] ID1=LON\851461 ID2=LON\47245 ID3=LON\456 ID4=LON\7333 ID5=LON\4456 [section 5] ID1=LON\66165 [section 6] ID1=LON\65835 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 28, 2009 Moderators Share Posted April 28, 2009 trwatters,I think this should do what you want. I have renamed the ini file for testing purposes (Vista does not like things in the C:\ root!) and changed a few variable names to the normal convention. The code is commented so I hope you can follow it without problem:expandcollapse popup#include <file.au3> #include <array.au3> $sAnswer = "Section 9" $sAdminRights = @ScriptDir & "\test.ini" ; Load existing section names into an array $aSectionList = IniReadSectionNames($sAdminRights) If Not IsArray($aSectionList) Then ; do something because the inifile does not exist! Else ; Look for our section If _ArraySearch ($aSectionList, $sAnswer) = -1 Then ; Do something because the section $sAnswer does not exist! MsgBox(0,"", $sAnswer & " does not exist") Else ; Ask - no exits immediately If MsgBox(4, "Open Remote.ini?", $sAnswer & "* Already Exists. Do you want to open Remote.ini?") = 7 Then Exit MsgBox(0,"", "Here we are running ComSpec") ;Run(@ComSpec & ' /c start ' & $sAdminRights, '', @SW_HIDE) EndIf EndIf ; Now we need to add to the ini so read in the key/value pairs in the $sAnswer section $aSectionKeyList = IniReadSection($sAdminRights, $sAnswer) If IsArray($aSectionKeyList) Then ; Section exists so add new key ; add an element to the array $iIndex = UBound($aSectionKeyList) Redim $aSectionKeyList[$iIndex + 1][2] ; Put the new values int eh array $aSectionKeyList[$iIndex][0] = "New_Key" $aSectionKeyList[$iIndex][1] = "New_Value" ; increase count $aSectionKeyList[0][0] += 1 ; Write the ini section IniWriteSection($sAdminRights, $sAnswer, $aSectionKeyList) Else ; Section does not exist so create it IniWriteSection($sAdminRights, $sAnswer, "New_Key=New_Value") EndIfOne of AutoIt's most useful features (IMHO) is the built-in ini file support - you can do lots of things to an ini file without much coding. Look in the Help file under Ini..... and you will soon see what I mean. :-)Adding blank lines to ini files is a bit more complicated - you might like to look here where it was discussed this a few days ago. It is not difficult, but does need a bit more coding.Ask if anything is unclear.M23 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...
WideBoyDixon Posted April 28, 2009 Share Posted April 28, 2009 (edited) I don't think your original array-based approach is too bad. Here's my stab at it: expandcollapse popup#include <file.au3> #include <array.au3> Global $answer = "Section 2" Global $valueToAdd = "LON\1212" Global $logArray Global $AdminRights = "C:\Remote.ini" If FileExists($AdminRights) Then _FileReadToArray($AdminRights, $logArray) Else Exit EndIf Global $result = _ArraySearch($logArray, "[" & $answer & "]") If $result < 0 Then Exit Global $sLast = "" While $logArray[$result] <> "" $sLast = $logArray[$result] $result += 1 If $result > $logArray[0] Then ExitLoop Wend Global $aSplit If $sLast = "" Then $sLast = "ID1" Else $aSplit = StringSplit($sLast, "=") $sLast = "ID" & String(Number(StringMid($aSplit[1], 3)) + 1) EndIf $sLast &= "=" & $valueToAdd If $result > $logArray[0] Then _ArrayAdd($logArray, $sLast) Else _ArrayInsert($logArray, $result, $sLast) EndIf $logArray[0] += 1 _FileWriteFromArray($AdminRights, $logArray, 1) Exit You'll notice that I'm a stickler for declaring variables. It helps to cut down on errors later. WBD EDIT: $sLast should be $aSplit[1] in the If statement ... now corrected. Edited April 28, 2009 by WideBoyDixon [center]Wide by name, Wide by nature and Wide by girth[u]Scripts[/u]{Hot Folders} {Screen Calipers} {Screen Crosshairs} {Cross-Process Subclassing} {GDI+ Clock} {ASCII Art Signatures}{Another GDI+ Clock} {Desktop Goldfish} {Game of Life} {3D Pie Chart} {Stock Tracker}[u]UDFs[/u]{_FileReplaceText} {_ArrayCompare} {_ToBase}~ My Scripts On Google Code ~[/center] Link to comment Share on other sites More sharing options...
kabar Posted April 28, 2009 Author Share Posted April 28, 2009 This is perfect. Exactly what I was looking for. Thank you. I bow to thee, wise one. 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