I've modified unexist03's code to give a list of sections, keys in a specified sections, or list of all keys in the ini file.
Usage:
Code:
ReadIni(Filename, Option, Option2 [, Section])
Filename:The name of the .ini file, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified.
Option:Sections - Returns an array of all sections in the ini file or number of sections.
Keys - Returns an array or number of the keys in a section or an array or number all keys in the ini file.
Option2:List - Returns an array of keys in a section, all keys in the file, or sections in the file.
Count - To be used to return the number of sections or keys.
Section:The section name in the .ini file, which is the heading phrase that appears
in square brackets (do not include the brackets in this parameter). Leave blank if you want to return all keys or sections in the file.
Examples:Get an array of all sections in test.ini
Code:
Array := ReadIni("test.ini","Sections","List")
Count the number of sections in test.ini
Code:
Array := ReadIni("test.ini","Sections","Count")
Get an array of all keys in test.ini
Code:
Array := ReadIni("test.ini","Keys","List")
Count the number of keys in test.ini
Code:
Array := ReadIni("test.ini","Keys","Count")
Get an array of all keys in the
General section.
Code:
Array := ReadIni("test.ini","Keys","List", "General")
Count the number of keys in the
General section.
Code:
Array := ReadIni("test.ini","Keys","Count", "General")
ReadIni.ahkCode:
; ---Read INI file
ReadIni(ri_filename,ri_option,ri_option2,ri_section = 0)
{
FileRead, ri_contents, %ri_filename%
if ri_option = Sections
{
ri_scount = 0
Loop, parse, ri_contents, `n, `r
{
StringLeft, 1stChar, A_LoopField, 1
; ---See if line is a section
if 1stChar = [
{
if ri_option2 = count
ri_scount+=1
if ri_option2 = list
{
; ---Count all sections
if (!ri_scount)
{
StringTrimLeft, var, A_LoopField, 1
StringTrimright, ri_scount,var,1
}
; ---Get array of all sections
else
{
StringTrimLeft, var, A_LoopField, 1
StringTrimright, var,var,1
ri_scount=%ri_scount%,%var%
}
}
}
}
return ri_scount
}
if ri_option = Keys
{
kcount=0
Loop, parse, ri_contents, `n, `r
{
StringLeft, 1stChar, A_LoopField, 1
var := RegExReplace(A_LoopField,"\=.*")
; ---If line is a section
if A_LoopField = [%ri_section%]
{
Line1 = %A_Index%
i++
}
; ---If line is empty and is after the specified section
if (Line1 && !A_LoopField && ri_section!=0)
{
Line2 = %A_Index%
kcount := Line2 - (Line1 + 1)
if ri_option2 = count
return kcount
if ri_option2 = list
return SecKeys
}
; ---If line is after section and is a key
if (i=1 && 1stChar <>"[")
{
; ---First string in array
If (!SecKeys)
SecKeys=%var%
; ---All other strings in array
else
SecKeys=%SecKeys%,%var%
}
; ---Get all keys and count them when sections variable is null
if (ri_section=0 && 1stChar!="[" && 1stChar!=";" && A_LoopField)
{
; ---First string in array
If !AllKeys
AllKeys=%var%
; ---All other strings in array
Else
AllKeys=%AllKeys%,%var%
kcount++
}
}
; ---Return list of all keys or number of all keys if sections var is null
if ri_option2 = list
Return, AllKeys
if ri_option2 = count
Return, kcount
}
; ---Clear variable
ri_contents =
}