Jump to content

[SOLVED]iniwrite overwriting values in for loop


Recommended Posts

I'm having some problems reading and writing to my ini. my goal is to check to see if a certain key name for example "Bob" exists in my ini file. if it does it will append to whatever value the key "Bob" has. my problem is it is looping through the ini file and sometimes it may not find that value exists at first so goes to Else statement. just a big mess. My question is what is the best way to search through a section for a specific key and if it is there then append to it like shown below, otherwise create the key and a value. i'd be really appreciative if someone could help me.

$var = "whatever"
$keycheck = IniReadSection($indicatordat, "section")    ; reads the section for all key/value pairs
                If @error Then 
                    MsgBox(0, "", "An error has occured, make sure the ini file exists.")
                Else
                    For $i = 1 To $keycheck[0][0]
                        If $keycheck[$i][0] = $var Then ; if there is already a key in the .ini file with the same name
                            IniWrite (@ScriptDir & "\test.ini", "section", $var, IniRead (@ScriptDir & "\test.ini", "section", $var, "") & "," & "test")  ; append test to whatever value exists in that key
                            ExitLoop
                        Else    ; no key with that name exists, make key with value test
                            IniWrite($indicatordat, "Player Indicators", $var, "test")
                        EndIf
                    Next
                EndIf
Edited by Hypertrophy
Link to comment
Share on other sites

The best way to search for a key is to use IniRead. If it returns the default, the key doesn't exist. Something like this?

$Ini = @ScriptDir & "\test.ini"
If IniRead($Ini, "section", "key", "") = "" Then
    IniWrite($Ini, "section", "key", "test")
Else
    IniWrite($Ini, "section", "key", IniRead($Ini, "section", "key", "") & ",test")
EndIf
Edited by dantay9
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...