Sign in to follow this  
Followers 0
SmOke_N

Ini Get Section Key And Value

11 posts in this topic

#1 ·  Posted (edited)

A bored thing again, it will return a 3 dimenisional array of all the sections / keys for the sections / and values for keys.

For $i = 1 To 6
    IniWrite(@DesktopDir & '\MyIniTest.ini', "section 1", "Key" & $i, "val" & $i)
Next
For $i = 1 To 3
    IniWrite(@DesktopDir & '\MyIniTest.ini', "section 2", "Key" & $i, "val" & $i)
Next
Global $IniInfo = _IniGetSKV(@DesktopDir & '\MyIniTest.ini')
_ArrayIniDisplay($IniInfo, 'IniInfo')
Func _IniGetSKV($hIniLocation)
    ;Get All Sections
    Local $aSections = IniReadSectionNames($hIniLocation)
    If @error Then Return SetError(1, 0, 0)
    ;Get All The Keys and Values for Each section
    Local $aKeyValues[$aSections[0] + 1][1][3], $nCount;Added $nCount
    For $iCount = 1 To $aSections[0]
        $aKV = IniReadSection($hIniLocation, $aSections[$iCount])
        If @error Then ; If empty section then only get name
            $aKeyValues[$iCount][0][0] = 0
            $aKeyValues[$iCount][1][0] = $aSections[$iCount]
            ContinueLoop
        EndIf
        If $nCount < $aKV[0][0] Then;Added this condition statement
            $nCount = $aKV[0][0]
            ReDim $aKeyValues[$aSections[0] + 1][$aKV[0][0] + 1][3]
        EndIf
        $aKeyValues[$iCount][0][0] = $aKV[0][0];Added this
        For $xCount = 1 To $aKV[0][0]
            $aKeyValues[$iCount][$xCount][0] = $aSections[$iCount]
            $aKeyValues[$iCount][$xCount][1] = $aKV[$xCount][0]
            $aKeyValues[$iCount][$xCount][2] = $aKV[$xCount][1]
        Next
    Next
    $aKeyValues[0][0][0] = $aSections[0]
    Return $aKeyValues ;Return a 3 Dimensional Array
EndFunc   ;==>_IniGetSKV


Func _ArrayIniDisplay($aArray, $sTitle = '')
    If Not IsArray($aArray) Then SetError(1, 0, 0)
    Local $sIni = '[0][0][0] = ' & $aArray[0][0][0] & ' Sections' & @CR
    For $xCC = 1 To $aArray[0][0][0]
        $sIni &= @CR & '[' & $xCC & '][0][0] = ' & $aArray[$xCC][0][0] & ' Keys/Values' & @CR & _
            '[' & $xCC & '][1][0] = ' & $aArray[$xCC][1][0] & @CR ; Get section name from first line
        For $aCC = 1 To $aArray[$xCC][0][0]
            $sIni &= '[' & $xCC & '][' & $aCC & '][1] = ' & $aArray[$xCC][$aCC][1] & @CR & _
                '[' & $xCC & '][' & $aCC & '][2] = ' & $aArray[$xCC][$aCC][2] & @CR
        Next
    Next
    MsgBox(0, $sTitle, StringTrimRight($sIni, 1))
EndFunc

Edit:

Added $aKeyValues[0][0][0] = Ubound($aKeyValues, 1) - 1 for Cue

Edit:

Updated with Dickb's additions for checking for "blank" sections.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites



Changed the way the return was, it wasn't returning all the elements correctly.

Also added an _ArrayIniDisplay() for this specifically so you can view and or trouble shoot if need be.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Intriguing... care to post your example file?

Share this post


Link to post
Share on other sites

Intriguing... care to post your example file?

If you run the above, it will make an example file :lmao:

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Made two changes to fix bugs

  • _IniGetSKV - handle empry sections
  • _ArrayIniDisplay - display section name when only one key/value
Global $IniInfo = _IniGetSKV(@ScriptDir & '\x.ini')
_ArrayIniDisplay($IniInfo, 'IniInfo')
Func _IniGetSKV($hIniLocation)
    ;Get All Sections
    Local $aSections = IniReadSectionNames($hIniLocation)
    If @error Then Return SetError(1, 0, 0)
    ;Get All The Keys and Values for Each section
    Local $aKeyValues[$aSections[0] + 1][1][3], $nCount;Added $nCount
    For $iCount = 1 To $aSections[0]
        $aKV = IniReadSection($hIniLocation, $aSections[$iCount])
        If @error Then ; If empty section then only get name
            $aKeyValues[$iCount][0][0] = 0
            $aKeyValues[$iCount][1][0] = $aSections[$iCount]
            ContinueLoop
        EndIf
        If $nCount < $aKV[0][0] Then;Added this condition statement
            $nCount = $aKV[0][0]
            ReDim $aKeyValues[$aSections[0] + 1][$aKV[0][0] + 1][3]
        EndIf
        $aKeyValues[$iCount][0][0] = $aKV[0][0];Added this
        For $xCount = 1 To $aKV[0][0]
            $aKeyValues[$iCount][$xCount][0] = $aSections[$iCount]
            $aKeyValues[$iCount][$xCount][1] = $aKV[$xCount][0]
            $aKeyValues[$iCount][$xCount][2] = $aKV[$xCount][1]
        Next
    Next
    $aKeyValues[0][0][0] = $aSections[0]
    Return $aKeyValues ;Return a 3 Dimensional Array
EndFunc   ;==>_IniGetSKV


Func _ArrayIniDisplay($aArray, $sTitle = '')
    If Not IsArray($aArray) Then SetError(1, 0, 0)
    Local $sIni = '[0][0][0] = ' & $aArray[0][0][0] & ' Sections' & @CR
    For $xCC = 1 To $aArray[0][0][0]
        $sIni &= @CR & '[' & $xCC & '][0][0] = ' & $aArray[$xCC][0][0] & ' Keys/Values' & @CR & _
            '[' & $xCC & '][1][0] = ' & $aArray[$xCC][1][0] & @CR ; Get section name from first line
        For $aCC = 1 To $aArray[$xCC][0][0]
            $sIni &= '[' & $xCC & '][' & $aCC & '][1] = ' & $aArray[$xCC][$aCC][1] & @CR & _
                '[' & $xCC & '][' & $aCC & '][2] = ' & $aArray[$xCC][$aCC][2] & @CR
        Next
    Next
    MsgBox(0, $sTitle, StringTrimRight($sIni, 1))
EndFunc

DickB

Share this post


Link to post
Share on other sites

Nice catch Dickb, I would have not thought of "empty" sections.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Nice catch Dickb, I would have not thought of "empty" sections.

Well, I used an existing ini file with an empty section.

The program crashed and that gave me the idea that there might be something wrong :lmao:

And I like a challenge, so I came up with this solution.

btw, thanks for the routine. It is usefull for me.

Share this post


Link to post
Share on other sites

I am not trying to hijack this thread, but this may be of interest, too...

http://www.autoitscript.com/forum/index.php?showtopic=26632 (post #4)

creates a dictionary with the prefix $dict_ (in the Global scope)

each $dict_ variable corresponds to a unique ini section, mapping it's keys to values

returns the number of dictionaries created (aka the number of sections in the ini file)

-John

Share this post


Link to post
Share on other sites

I am not trying to hijack this thread, but this may be of interest, too...

http://www.autoitscript.com/forum/index.php?showtopic=26632 (post #4)

-John

I'm not trying to hijack but here's the thread? :lmao: J/K :ph34r: but I've always found that line humorous.

There's an issue with yours I believe in the StringStripWs(), you alter the text return that way and don't get a true return. That should be a user option not hard coded IMHO.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

There's an issue with yours I believe in the StringStripWs(), you alter the text return that way and don't get a true return. That should be a user option not hard coded IMHO.

But you can't have spaces in a variable name. If you had a section called [web browser settings], then the variable the script would try to create would be: $dict_web browser settings

Using StringStripWS it creates $dict_webbrowsersettings

-John

Share this post


Link to post
Share on other sites

But you can't have spaces in a variable name. If you had a section called [web browser settings], then the variable the script would try to create would be: $dict_web browser settings

Using StringStripWS it creates $dict_webbrowsersettings

-John

My mistake John, I didn't look entirely at what you were doing :lmao:

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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