Jump to content
Sign in to follow this  
kabar

Append to the end of a [Section] in an INI

Recommended Posts

kabar

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

Share this post


Link to post
Share on other sites
Melba23

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:

#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")
EndIf

One 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._______My UDFs:

Spoiler

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
WideBoyDixon

I don't think your original array-based approach is too bad. Here's my stab at it:

#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 by WideBoyDixon

Share this post


Link to post
Share on other sites
kabar

This is perfect. Exactly what I was looking for. Thank you.

I bow to thee, wise one. ^_^

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.