Jump to content

write to ini with decreased value


deve
 Share

Recommended Posts

I am trying to write some entries to ini file this is my code :

Case $Kaydet
                $LCOUNT = IniRead($sGuvercinINI, "Guvercin Kayıt", "", "")
                $answerOne = GUICtrlRead($Kunye)
                $answer2 = GUICtrlRead($Anne)
                  $answer3 = GUICtrlRead($Baba)
                  $answer4 = GUICtrlRead($Cinsi)
                  $answer5 = GUICtrlRead($Renk)
                  $answer6 = GUICtrlRead($Dogum)
                  $answer7 = GUICtrlRead($Pic1)
                  $answer8 = GUICtrlRead($Combo2)
                  $answer9 = GUICtrlRead($Notlar)
                  $LCOUNT=$LCOUNT+1
                  IniWrite($sGuvercinINI, $LCOUNT, "Künye", "")
                If IniRead($sGuvercinINI, "", "Künye", "") <> $answerOne Then
                  IniWrite($sGuvercinINI, $LCOUNT, "Künye", $answerOne)
                  IniWrite($sGuvercinINI, $LCOUNT, "Annesi", $answer2)
                  IniWrite($sGuvercinINI, $LCOUNT, "Babası", $answer3)
                  IniWrite($sGuvercinINI, $LCOUNT, "Cinsi", $answer4)
                  IniWrite($sGuvercinINI, $LCOUNT, "Rengi", $answer5)
                  IniWrite($sGuvercinINI, $LCOUNT, "Doğum", $answer6)
                  IniWrite($sGuvercinINI, $LCOUNT, "Resmi", $answer7)
                  IniWrite($sGuvercinINI, $LCOUNT, "Durumu", $answer8)
                  IniWrite($sGuvercinINI, $LCOUNT, "Not", $answer9)

                Else
                    MsgBox($MB_APPLMODAL, "", "Bu künye ile zaten başka bir kuş kayıtlı " & $answerOne)
                 EndIf
                 EndSwitch

my ini output is :

 

[1]
Künye=adsada
Annesi=
Babası=
Cinsi=
Rengi=
Doğum=
Resmi=
Durumu=Durum Seç
Not=Notlar

so far everything is ok but when I enter new entries the script jusg updates the new entrie , I wont it to record us new entry like :

[1]
Künye=adsada
Annesi=
Babası=
Cinsi=
Rengi=
Doğum=
Resmi=
Durumu=Durum Seç
Not=Notlar
[2]
Künye=deve
Annesi=
Babası=
Cinsi=
Rengi=
Doğum=
Resmi=
Durumu=Durum Seç
Not=Notlar

 

Edited by deve
Link to comment
Share on other sites

deve,

If IniWrite finds a duplicate key in whatever section you are writing to, it will update that key, not add a new key.  You might want to explain your task and/or post a reproducer for further assistance.

kylomas

P.S. A reproducer is "runnable" code.

edit: dump $LCOUNT to the console or MSGBOX after the IniRead.  I think you'll find it does not contain what you think it does.

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

I am new to autoit this codes I toke from other topics they maybee not correct but what is the correct code to increase the added infos count

like this :

[1]
Künye=adsada
Annesi=
Babası=
Cinsi=
Rengi=
Doğum=
Resmi=
Durumu=Durum Seç
Not=Notlar
[2]    <<<< new entry gets number 2  if the key Künye is same value it must popup msg and say this value is allready recorded.
Künye=deve
Annesi=
Babası=
Cinsi=
Rengi=
Doğum=
Resmi=
Durumu=Durum Seç
Not=Notlar

 

 

Link to comment
Share on other sites

Have a careful look to all the Ini* funcs in the helpfile
Assuming that the ini contains only sections [1], [2], ...[n] something like this should work :

$sections = IniReadSectionNames($sGuvercinINI)   ; number of sections in $sections[0]

Local $value_exists = 0
For $i = 1 to $sections[0]  ; loop through sections, looking for the value
     If IniRead($sGuvercinINI, $sections[$i], "Künye", "") = $answerOne Then 
          $value_exists = $i
          Exitloop
     EndIf
Next

If $value_exists > 0 Then   ; the value was found
      Msgbox(0,"", "this value is already recorded in section " & $sections[$value_exists])
      ; ...
Else   ; value not found
      $newsection = $sections[0]+1   ; add a new section
      IniWrite($sGuvercinINI, $newsection, "...", "...")
      ; etc
EndIf

 

Link to comment
Share on other sites

I

Have a careful look to all the Ini* funcs in the helpfile
Assuming that the ini contains only sections [1], [2], ...[n] something like this should work :

$sections = IniReadSectionNames($sGuvercinINI)   ; number of sections in $sections[0]

Local $value_exists = 0
For $i = 1 to $sections[0]  ; loop through sections, looking for the value
     If IniRead($sGuvercinINI, $sections[$i], "Künye", "") = $answerOne Then 
          $value_exists = $i
          Exitloop
     EndIf
Next

If $value_exists > 0 Then   ; the value was found
      Msgbox(0,"", "this value is already recorded in section " & $sections[$value_exists])
      ; ...
Else   ; value not found
      $newsection = $sections[0]+1   ; add a new section
      IniWrite($sGuvercinINI, $newsection, "...", "...")
      ; etc
EndIf

 

When I use this I have this error : Subscript used on non-accessible variable.:

in For $i = 1 to $sections[0]  ; loop through sections, looking for the value

 

mh code is like this :

 Case $Kaydet
                $answerOne = GUICtrlRead($Kunye)
                $answer2 = GUICtrlRead($Anne)
                  $answer3 = GUICtrlRead($Baba)
                  $answer4 = GUICtrlRead($Cinsi)
                  $answer5 = GUICtrlRead($Renk)
                  $answer6 = GUICtrlRead($Dogum)
                  $answer7 = GUICtrlRead($hInput8)
                  $answer8 = GUICtrlRead($Combo2)
                  $answer9 = GUICtrlRead($Notlar)
$sections = IniReadSectionNames($sGuvercinINI)   ; number of sections in $sections[0]
Local $value_exists = 0
For $i = 1 to $sections[0]  ; loop through sections, looking for the value
     If IniRead($sGuvercinINI, $sections[$i], "Künye", "") = $answerOne Then
          $value_exists = $i
          Exitloop
     EndIf
Next
If $value_exists > 0 Then   ; the value was found
      Msgbox(0,"", "this value is already recorded in section " & $sections[$value_exists])
      ; ...
Else   ; value not found
      $newsection = $sections[0]+1   ; add a new section
      IniWrite($sGuvercinINI, $answerOne, "", "")
       IniWrite($sGuvercinINI, $answer2, "", "")
        IniWrite($sGuvercinINI, $answer3, "", "")
         IniWrite($sGuvercinINI, $answer4, "", "")
          IniWrite($sGuvercinINI, $answer5, "", "")
           IniWrite($sGuvercinINI, $answer6, "", "")
            IniWrite($sGuvercinINI, $answer7, "", "")
             IniWrite($sGuvercinINI, $answer8, "", "")
              IniWrite($sGuvercinINI, $answer9, "", "")
      ; etc
EndIf

 

Edited by deve
Link to comment
Share on other sites

Hmmm either the $sGuvercinINI var is not correctly defined, or the ini file is empty (no section inside)

$sections = IniReadSectionNames($sGuvercinINI)   ; number of sections in $sections[0]

If @error Then ; ini file is empty
    $newsection = 1   ; add the first section
    IniWrite($sGuvercinINI, $newsection, "...", "...")
    ; etc
Else
   Local $value_exists = 0
   For $i = 1 to $sections[0]  ; loop through sections, looking for the value
      If IniRead($sGuvercinINI, $sections[$i], "Künye", "") = $answerOne Then 
          $value_exists = $i
          Exitloop
      EndIf
   Next
   If $value_exists > 0 Then   ; the value was found
      Msgbox(0,"", "this value is already recorded in section " & $sections[$value_exists])
      ; ...
   Else   ; value not found
      $newsection = $sections[0]+1   ; add a new section
      IniWrite($sGuvercinINI, $newsection, "...", "...")
      ; etc
   EndIf
EndIf

BTW the IniWrite should be written like this

IniWrite($sGuvercinINI, $newsection, "Künye", $answerOne)
; etc

 

Edited by mikell
Link to comment
Share on other sites

Just a quick look...try

 $i = 1 to $sections[0]  -1

8)

that worked thanks ,

many thanks mikell

now I need to read just 4 of the keys in ListView :( another question

little problem when the ini file is empty it give me the same erorr :(

Edited by deve
Link to comment
Share on other sites

Did you try the code from post #7  ?

the first record comes out in ini like this allways :

[1]
...=...  

second record with the keys and values correct and why is my save button not correctly working I have to click on it sometimes 5 times to get it work?

 

 

$sections = IniReadSectionNames($sGuvercinINI)   ; number of sections in $sections[0]

If @error Then ; ini file is empty
    $newsection = 1   ; add the first section
    IniWrite($sGuvercinINI, $newsection, "...", "...")
    ; etc
Else
   Local $value_exists = 0
   For $i = 1 to $sections[0]  ; loop through sections, looking for the value
      If IniRead($sGuvercinINI, $sections[$i], "Künye", "") = $answerOne Then
          $value_exists = $i
          Exitloop
      EndIf
   Next
   If $value_exists > 0 Then   ; the value was found
      Msgbox(0,"", "this value is already recorded in section " & $sections[$value_exists])
      ; ...
   Else   ; value not found

      $newsection = $sections[0]+1   ; add a new section
     ; IniWrite($sGuvercinINI, $newsection, "", "")
      IniWrite($sGuvercinINI, $newsection, "Künye", $answerOne)
       IniWrite($sGuvercinINI, $newsection, "Anne", $answer2)
        IniWrite($sGuvercinINI, $newsection, "Baba", $answer3)
         IniWrite($sGuvercinINI, $newsection, "Cinsi", $answer4)
          IniWrite($sGuvercinINI, $newsection, "Renk", $answer5)
           IniWrite($sGuvercinINI, $newsection, "Doğum", $answer6)
            IniWrite($sGuvercinINI, $newsection, "Resmi", $answer7)
             IniWrite($sGuvercinINI, $newsection, "Durumu", $answer8)
              IniWrite($sGuvercinINI, $newsection, "Notlar", $answer9)
      ; etc
   EndIf
   EndIf

 

Edited by deve
Link to comment
Share on other sites

the first record comes out in ini like this allways :

[1]
...=...  

If @error Then    ; ini file is empty
    $newsection = 1     ; add the first section ( section [1] )
    IniWrite($sGuvercinINI, $newsection, "...", "...") ; <<<<<<<<<<<<<<<<<<<<
     ; Obviously this was just a sample line and you must put instead the whole bunch of IniWrite
    ; etc
Else
....

Never heard of a save button. This is new ?

Link to comment
Share on other sites

Hi.

I do such things as follows:

  • write to the ini what's needet
  • use inireadsection to get all section names
  • read each section's content, sort it, write back to the ini the "sorting result"
#include <array.au3>

DirCreate("C:\temp")
$INI="C:\temp\my.ini"


Dim $aData[5][2]=[[4], _
    ["Zappa","value1"], _
    ["Planet","Earth"], _
    ["The Answer","42"], _
    ["Book","THHGTTG:DP"]]


$Sect="1"

for $i = 1 to $aData[0][0]
    IniWrite($INI,$Sect,$aData[$i][0],$aData[$i][1])
Next

$Sect="2"

for $i = $aData[0][0] to 1 Step -1 ;reverse initial order
    IniWrite($INI,$Sect,$aData[$i][0],$aData[$i][1])
Next


$aSections=IniReadSectionNames($INI)
if IsArray($aSections) Then
    MsgBox(0,"OK", "INI " & $INI & " has sections")
    for $s=1 to $aSections[0]
        $aContent=IniReadSection($INI,$aSections[$s])
        _ArrayDisplay($aContent,$aSections[$s] & ": found order")
        _ArraySort($aContent)
        _ArrayDisplay($aContent,$aSections[$s] & ": Sorted (Collumn 1)")
        IniWriteSection($INI,$aSections[$s],$aContent)
    Next
Else
    MsgBox(48,"ERR","INI " & $ini & " doesn't exist or has no sections.")
EndIf

Run ("notepad " & $ini)

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

If @error Then    ; ini file is empty
    $newsection = 1     ; add the first section ( section [1] )
    IniWrite($sGuvercinINI, $newsection, "...", "...") ; <<<<<<<<<<<<<<<<<<<<
     ; Obviously this was just a sample line and you must put instead the whole bunch of IniWrite
    ; etc
Else
....

Never heard of a save button. This is new ?

this is final code I have and it works now :

Case $Kaydet
$answerOne = GUICtrlRead($Kunye)
$answer2 = GUICtrlRead($Anne)
$answer3 = GUICtrlRead($Baba)
$answer4 = GUICtrlRead($Cinsi)
$answer5 = GUICtrlRead($Renk)
$answer6 = GUICtrlRead($Dogum)
$answer7 = GUICtrlRead($hInput8)
$answer8 = GUICtrlRead($Combo2)
$answer9 = GUICtrlRead($Notlar)
$sections = IniReadSectionNames($sGuvercinINI)   ; number of sections in $sections[0]
If @error Then ; ini file is empty
       $newsection = 1     ; add the first section ( section [1] )
IniWrite($sGuvercinINI, $newsection, "Künye", $answerOne)
IniWrite($sGuvercinINI, $newsection, "Anne", $answer2)
IniWrite($sGuvercinINI, $newsection, "Baba", $answer3)
IniWrite($sGuvercinINI, $newsection, "Cinsi", $answer4)
IniWrite($sGuvercinINI, $newsection, "Renk", $answer5)
IniWrite($sGuvercinINI, $newsection, "Doğum", $answer6)
IniWrite($sGuvercinINI, $newsection, "Resmi", $answer7)
IniWrite($sGuvercinINI, $newsection, "Durumu", $answer8)
IniWrite($sGuvercinINI, $newsection, "Notlar", $answer9)
     ; Obviously this was just a sample line and you must put instead the whole bunch of IniWrite
    ; etc
Else
Local $value_exists = 0
For $i = 1 to $sections[0]  ; loop through sections, looking for the value
If IniRead($sGuvercinINI, $sections[$i], "Künye", "") = $answerOne Then
$value_exists = $i
Exitloop
EndIf
Next
If $value_exists > 0 Then   ; the value was found
Msgbox(0,"", "this value is already recorded in section " & $sections[$value_exists])
Else
$newsection = $sections[0]+1   ; add a new section
IniWrite($sGuvercinINI, $newsection, "Künye", $answerOne)
IniWrite($sGuvercinINI, $newsection, "Anne", $answer2)
IniWrite($sGuvercinINI, $newsection, "Baba", $answer3)
IniWrite($sGuvercinINI, $newsection, "Cinsi", $answer4)
IniWrite($sGuvercinINI, $newsection, "Renk", $answer5)
IniWrite($sGuvercinINI, $newsection, "Doğum", $answer6)
IniWrite($sGuvercinINI, $newsection, "Resmi", $answer7)
IniWrite($sGuvercinINI, $newsection, "Durumu", $answer8)
IniWrite($sGuvercinINI, $newsection, "Notlar", $answer9)
EndIf
EndIf

save button is

Case $Kaydet  << this is a button in GUI called $Kaydet when I click on this it runs the script but like I sayed before I need to click sometimes 5 times to get it work
$Kaydet = GUICtrlCreateButton("Kaydet", 847, 612, 105, 25, $BS_DEFPUSHBUTTON)
GUICtrlSetColor(-1, 0x000000)
GUICtrlSetBkColor(-1, 0xFF8000)

 

Link to comment
Share on other sites

There is probably elsewhere in the script something slowing down the main While loop

yes I guess also like this well how can I read this keys in listview now :) just 4 of them I need to read right after record to listview . I have managed to read just one section keys but the others I chouldnt , I done like this :

Func _GetAccounts()
Local $aAccounts = IniRead(@ScriptDir & '\guvercin.ini', "2", "Künye", "")
Local $bAccounts = IniRead(@ScriptDir & '\guvercin.ini', "2", "Cinsi", "")
Local $cAccounts = IniRead(@ScriptDir & '\guvercin.ini', "2", "Doğum", "")
Local $dAccounts = IniRead(@ScriptDir & '\guvercin.ini', "2", "Durumu", "")
If Not @error Then
GUICtrlSetData($idItem1, $aAccounts)
GUICtrlSetData($idItem1, "|" & $bAccounts)
GUICtrlSetData($idItem1, "||" & $cAccounts)
GUICtrlSetData($idItem1, "|||" & $dAccounts)
EndIf
EndFunc

this way I can read only in section 2 the values need to read all sections

Link to comment
Share on other sites

GUICtrlSetData($idItem1, $aAccounts & "|" & $bAccounts & "|" & $cAccounts & "|" & $dAccounts)

BTW you know what ? the help file is of great help  :yes:

thanks for to make it shorter , between my english makes it hard to understand and find things in the help file , but many thanks to you now I am able to write to ini y project is half finish now but :) the read to listview is just for the Section 2 the other sections doesnt get read

Local $aAccounts = IniRead(@ScriptDir & '\guvercin.ini', "2", "Künye", "")  <<<< this 2 what do I need to change to make it loop to all sections and read them to listview
Link to comment
Share on other sites

Man you understand of course that I can't write your whole script - especially because I must go to bed soon  :D

Here are clues for doing what you want, you will have to adapt and integrate this to the script
Good luck  ;)

Global $aItems   ; declare array to store the IDs of the listview items

Func _Fill_Listview()
   $sGuvercinINI = @ScriptDir & '\guvercin.ini'
   $sections = IniReadSectionNames($sGuvercinINI) ; get section names
If not @error Then 
 GuiCtrlSendMsg($listview, $LVM_DELETEALLITEMS, 0, 0)  ; clean the listview
 Dim $aItems[$sections[0]]  ; create and size the items array
 For $i = 1 to $sections[0]   ; loop through sections
   Local $aAccounts = IniRead($sGuvercinINI, $sections[$i], "Künye", "")
   Local $bAccounts = IniRead($sGuvercinINI, $sections[$i], "Cinsi", "")
   Local $cAccounts = IniRead($sGuvercinINI, $sections[$i], "Dog(um", "") ; << typo here !!
   Local $dAccounts = IniRead($sGuvercinINI, $sections[$i], "Durumu", "")
  ; create listview items 
  $aItems[$i-1] = GUICtrlCreateListViewItem($aAccounts & "|" & $bAccounts & "|" & $cAccounts & "|" & $dAccounts, $listview)
  GuiCtrlSetOnEvent($aItems[$i-1], "_display")
 Next
EndIf
EndFunc


Func _display()
   For $i = 0 to UBound($aItems)-1   ; loop through the item IDs
      If @GUI_CtrlId = $aItems[$i] Then  ; the item was clicked
       ; here put the instructions to display the image and infos for this item
      EndIf
   Next
EndFunc

 

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...