Hypertrophy Posted July 8, 2009 Share Posted July 8, 2009 (edited) 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 July 8, 2009 by Hypertrophy Link to comment Share on other sites More sharing options...
dantay9 Posted July 8, 2009 Share Posted July 8, 2009 (edited) 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 July 8, 2009 by dantay9 Link to comment Share on other sites More sharing options...
Hypertrophy Posted July 8, 2009 Author Share Posted July 8, 2009 Thank you! Much simpler and more effective than my useless For loop 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