Determine the Section in an ini file from a known key and value

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Determine the Section in an ini file from a known key and value

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

highend
Posts: 14925
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Determine the Section in an ini file from a known key and value

Post 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!"; }
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: Determine the Section in an ini file from a known key and value

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

highend
Posts: 14925
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Determine the Section in an ini file from a known key and value

Post by highend »

I was just showing a straightforward way.
The fastest possible one is:
Remove the inner loop and use a regex instead…
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 3446
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Determine the Section in an ini file from a known key and value

Post 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; } }

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: Determine the Section in an ini file from a known key and value

Post 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
   }

highend
Posts: 14925
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Determine the Section in an ini file from a known key and value

Post 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, "^\[.+?\]"); }
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: Determine the Section in an ini file from a known key and value

Post 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, "^\[.+?\]"); }

highend
Posts: 14925
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Determine the Section in an ini file from a known key and value

Post by highend »

You are reading the .dbits file here, not the .ini :whistle:

Code: Select all

    $ini = replace(get("thumbs_cache"), ".dbits", ".ini");
One of my scripts helped you out? Please donate via Paypal

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: Determine the Section in an ini file from a known key and value

Post by klownboy »

Sorry highend, I had the exact same SC replace statement in my version above but forgot to put it in yours. :roll: Works great, thanks.

Post Reply