Page 1 of 1
Determine the Section in an ini file from a known key and value
Posted: 08 Sep 2024 21:20
by klownboy
Is there a method to determine the "section" of the ini file when both the "key" and its "value" are known?
As an example, the thumbnail cache ini files have a [General] section and number of [Thumb X] sections depending on the number of cached images in the folder.
Code: Select all
[Thumb 2]
Width=240
Height=160
WidthO=4896
HeightO=3264
Dbits=28800
DbitsHi=0
DbitsC=28800
Slot=28800
Flags=2
FlagsB=0
Mod=2013-11-02 20:46:40
File=DSCF2655.JPG
I have the key "File" and its value "DSCF2655.JPG". I want to determine the Section where the File value is located, in this case [Thumb 2]. The reason is to change the date value of Mod. I haven't found anything that will do it directly or indirectly in documented or undocumented commands and functions. Thanks.
Re: Determine the Section in an ini file from a known key and value
Posted: 08 Sep 2024 22:42
by highend
Code: Select all
$find = 'DSCF2655.JPG';
$inis = quicksearch("*.ini", <xythumbs>);
end (!$inis), "No .ini file(s) found, aborted!";
$known = "";
foreach($ini, $inis, <crlf>, "e") {
$sections = getsectionlist(, $ini);
foreach($section, $sections, <crlf>, "e") {
$keyVals = getsectionlist($section, $ini);
if (strpos($keyVals, $find) != -1) {
$known .= $ini . "|" . $section . "|" . $find . <crlf>;
}
}
}
if ($known) { text $known; }
else { e "Not found!"; }
Re: Determine the Section in an ini file from a known key and value
Posted: 08 Sep 2024 23:16
by klownboy
Hey highend. I won't have time to check it out until tomorrow. In the meantime, I had worked out a method also, though probably more cumbersome in some ways but more direct in others. I'll get back. Thanks.
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 00:56
by highend
I was just showing a straightforward way.
The fastest possible one is:
Remove the inner loop and use a regex instead…
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 05:53
by jupe
Alt.
Code: Select all
$key = "File";
$val = "DSCF2655.JPG";
$ini = "<xythumbs>\ini_filename.ini";
foreach($s, getsectionlist(, $ini), <crlf>) { if (getkey($key, $s, $ini) == $val) { e $s; break; } }
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 15:08
by klownboy
Thanks highend and jupe. I tested both methods in my scenario and they work fine. More straightforward than what I came up with as shown below.
The script is intended to work with selected files in the current image folder. So, I'm able to determine the associated cache file(s) easily. I had the not so brilliant idea of updating the "Mod" key in the [Thumb #] section. Only because, if XYplorer detects a change in the image file's modified date, it will rewrite the thumbnail(s) (dbits file) which seems unnecessary if you
know for a fact that it was only the date that changed and not the actual image. In that case, the associated thumbnail shouldn't have to be updated. Like I said it was a bit whacky. Thanks again.
Code: Select all
$cachefile = get("thumbs_cache"); //cache file current folder
$cachefile_ini = replace($cachefile, "dbits", "ini"); //associated cache ini
$ini = readfile($cachefile_ini);
foreach($image, <selitems <crlf>>, <crlf>, "e") {
$image_name = gpc($image, "name");
$pos = strpos($ini, "File" . "=" . $image_name);
$ini_new = readfile($cachefile_ini,, 50,,$pos);
$thumb = gettoken($ini_new, 3, <crlf>);
if($thumb) {
$thumb = substr($thumb, 7, -1)-1;}
else {
$thumb = getkey("CountThumbs", "General",$cachefile_ini);} // for the last thumb in ini sections
}
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 16:32
by highend
If you have selected such an .ini file, this is the fastest way:
Code: Select all
$find = "IMG_20200224_173205.jpg";
$match = regexmatches(readfile(), "^\[.+?\]\s*[^[]*?^File=" . regexreplace($find, "([\\^$.+()\[{])", "\$1"));
if ($match) { e regexmatches($match, "^\[.+?\]"); }
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 18:00
by klownboy
I wouldn't waste any more time on this highend. I already have 3 methods of accomplishing it.

My whole idea as I mentioned was to avoid having XYplorer rewrite the "dbit" cache file when only the modified date changed. For example, if I used File > File Special > Set Modified Date to Exif for some image files. Only the modified date for those images has changed, but XYplorer sees that as an image change and probably has to (yes, the date changed but the actual image does not).
In any case I tried that regex version though I had to modify it slightly since the current file wouldn't be the cache ini, it would be the image file itself. So I modified the input for the regex but it doesn't seem to work for me. Thanks again.
Code: Select all
$ini = get("thumbs_cache");
$find = gpc(<curitem>, "name");
$match = regexmatches(readfile($ini), "^\[.+?\]\s*[^[]*?^File=" . regexreplace($find, "([\\^$.+()\[{])", "\$1"));
if ($match) { e regexmatches($match, "^\[.+?\]"); }
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 19:48
by highend
You are reading the .dbits file here, not the .ini
Code: Select all
$ini = replace(get("thumbs_cache"), ".dbits", ".ini");
Re: Determine the Section in an ini file from a known key and value
Posted: 09 Sep 2024 20:46
by klownboy
Sorry highend, I had the exact same SC replace statement in my version above but forgot to put it in yours.

Works great, thanks.