Page 1 of 1

Howto: Enumerate INI Sections/Keys/Values from an INI file

Posted: 20 Jul 2014 17:58
by neil9090
This is how you can enumerate INI sections/keys and key values.

I found in a forum item how to get the first section in the INI, which was good, but then I found out the that returned getkey value also included any other section names. This was also true to keys which means you can also get all of the values as well.
The values returned by getkey are separated by chr(0) but also includes chr(0) at the beginning thus the trim commands.

e.g. example.ini
[Section1]
Key1=Key1Value
[Section2]
Key2=Key2Value

Simple but if you didn't know that of the section2 name this would be a problem

I couldn't find an existing topic so just thought I would post it.

Code: Select all

	// Enumerate INI Sections/Keys/Values

	$GEN_Ini = self("path")."\".self("base").".ini"; // ini file
//	$GEN_Ini adjust accordingly to suit needs

	$tempstring = "";
	$Sections = trim(getkey("", "", $GEN_Ini),chr(0));
		
	foreach($section, $Sections, chr(0))
	{
		$Keys = trim(getkey("", $section, $GEN_Ini),chr(0));
		foreach($key, $Keys, chr(0))
		{
			$tempstring = $tempstring . "Section : " . $section . "." . $key .
				"=>" . getkey($key, $section, $GEN_Ini) .
				"<crlf>";
		}		
	}
        // output found values for demonstration purposes...
	msg $tempstring; // no scrollbar
	text $tempstring; // popup box
	echo $tempstring;

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 07:07
by bdeshi
Nice! :appl:

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:01
by klownboy
Yes, very nice and I just learned something. "msg" doesn't provide an vertical scroll bar for a long message so I suppose we should use text or echo which does for cases like this. 8)

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:07
by SkyFrontier
$GEN_Ini = "<xydata>\<xyini>"; // ini file
seems to be a safer choice, guys.

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:11
by TheQwerty
SkyFrontier wrote:$GEN_Ini = "<xydata>\<xyini>"; // ini file
seems to be a safer choice, guys.
Only if you actually want XYplorer's INI file - neil9090's reading in an INI file that has the same path and name as the script file.
klownboy wrote:we should use text or echo which does for cases like this. 8)
Echo should be synonymous with msg so it wouldn't show scrollbars either. ;)

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:25
by bdeshi
Actually echo does show a scrollbar. Blame genetics. (msg not being the same as it's little brother)

Anyways, it makes more sense for the result to be used as some kind of script input, instead of displaying it...

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:35
by SkyFrontier
Thanks for the note, TQ!

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:43
by klownboy
TheQwerty wrote:Echo should be synonymous with msg so it wouldn't show scrollbars either
I too have a scroll bar with echo but none with msg. I wonder if it's expected and possibly a bug. Certainly no biggie, typically you wouldn't use msg for a case like this anyway or as Sammay mentioned you'd have the results returned to the script.

Re: Howto: Enumerate INI Sections/Keys/Values from an INI fi

Posted: 21 Jul 2014 14:49
by TheQwerty
klownboy wrote:
TheQwerty wrote:Echo should be synonymous with msg so it wouldn't show scrollbars either
I too have a scroll bar with echo but none with msg. I wonder if it's expected and possibly a bug. Certainly no biggie, typically you wouldn't use msg for a case like this anyway or as Sammay mentioned you'd have the results returned to the script.
I don't think it's a bug, just my mistake. I always thought they were one and the same but indeed the documentation calls echo msg's smaller brother so the differences are correct.