Sign in to follow this  
Followers 0
Hypertrophy

[SOLVED]iniwrite overwriting values in for loop

3 posts in this topic

#1 ·  Posted (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 by Hypertrophy

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by dantay9

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

Thank you! Much simpler and more effective than my useless For loop :)

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  
Followers 0