Test a section/key entry in .ini for existance

Features wanted...
PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Test a section/key entry in .ini for existance

Post by PeterH »

You know, XY says never to *add* a key to XYplorer.ini - only to change existing keys. But how to test for existance of a key?

getkey() for a non-existant key returns "" - same as for existant but empty (= no value) key.

If I'm right I have a wish: let the script recognize if getkey() didn't find a (section/)key. Maybe like some functions allow to specify an operand cancel= [eg input() or inputselect()], so that getkey() returns some recognizable value for "non-exist".

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

Re: Test a section/key entry in .ini for existance

Post by highend »

Code: Select all

if !(regexmatches(getsectionlist("<section>"), "^<key>=")) { true if <key> doesn't exist... }
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: Test a section/key entry in .ini for existance

Post by klownboy »

I think you need a another closing ")", but from my testing with this "INI" section

Code: Select all

[GENERAL]
Order=Normal|Guitar Video|Pictures and Wallpaper|Dual - Pics and Walls|Links and Catalog|Movies-Video-TV
Favorite=
CurrentSession=Normal
CTBindex=5
FolderChangeOnlyLocations=Guitar Video|Links and Catalog|Movies-Video-TV|Normal
The result is the same if you have no "key" for "Favorite" or there is no "Favorite" line in the ini file at all. PeterH wants to know to be able to detect if there is a blank for "Favorite=" key or the Favorite line does not exist. Currently they do return the same when using SC getkey as he mentioned.

EDIT: Scratch that highend. It works fine with the additional parenthesis. :) Though it still might be worthwhile to have a built-in distinction between an empty key value and a missing key entirely.

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

Re: Test a section/key entry in .ini for existance

Post by highend »

I don't see a problem here either...

Code: Select all

    $section = "General";
    $key     = "NewPath"; // Try it e.g. with "NewPath | DPIs" (the latter doesn't exist)
    if (isKey($section, $key))    { msg "key exists!"; }
    if (hasValue($section, $key)) { msg "val exists!"; }

function isKey($section, $key) {
    $isKey = regexmatches(getsectionlist($section), "^" . $key . "=.*?(?=\r?\n|$)");
    if ($isKey) { return true; }
    return false;
}

function hasValue($section, $key) {
    $isKey = regexmatches(getsectionlist($section), "^" . $key . "=.*?(?=\r?\n|$)");
    if ($isKey && gettoken($isKey, 2, "=", "t", 2) != "") { return true; }
    return false;
}
One of my scripts helped you out? Please donate via Paypal

mazot
Posts: 48
Joined: 20 Apr 2020 23:19

Re: Test a section/key entry in .ini for existance

Post by mazot »

I don't know if it is worth your attention but the command "find" does a very good job

Code: Select all

        $cmdline = <<<CMD_LINE
cmd /c Find /I  "Favorite" "C:\Users\____\Documents\workingOnNow\genini.txt"
CMD_LINE;
         $cmdline = Replace($cmdline, "<crlf>", ' ');
        $a= Runret($cmdline, , 0, 0);
          wait 500;
          }
          text $a;

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Test a section/key entry in .ini for existance

Post by PeterH »

First thanks to all for their ideas. :tup: Now I'm convinced that this can be done by scripting. But by executing quite some commands, and I/Os. (Have tested a bit on highends code, though with no (no!) knowledge about regex...!)

But! This will be needed for quite a list of section/key combinations, maybe over several different .ini-files. And in relation to a little bit code in XY's getkey() to give info about missing keys it will be *very* slow. (A getkey() will generally be needed - so the info about non-existance can be delivered with *no* additional I/O at all.)

So my hope is on Don, adding this as extension to getkey().

Again thanks to all: it's always good to see ideas of others! That's always something you can learn and get own ideas from.

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

Re: Test a section/key entry in .ini for existance

Post by highend »

You could easily modify it to just read the full content for a .ini file once and then get what you need. This wouldn't be slow at all...
One of my scripts helped you out? Please donate via Paypal

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Test a section/key entry in .ini for existance

Post by PeterH »

I can't believe that.

One side: when getkey() reads a section/key and doesn't find it:
instead of just returning "":
- test for a (new: 5th) parm to getkey()
- if found return that
- else return "" like it's now

The other:
- before getkeys (for 1 .ini) the script once reads the complete XYplorer.ini (or other) to memory
- for every sect/key it searches this complete .ini for the sect/key for fit
If I missed something: I think it's enough...
OK: optimization:
- read (or split) .ini to several variables with 1 sect each,
- so search only the needed sect-variable for the key.

Hm.

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

Re: Test a section/key entry in .ini for existance

Post by klownboy »

Great highend. Thanks. I wanted to return only one condition (or message) and use one function so I revamped it a little and used switch/case.

Code: Select all

    $section = "General";
    $key     = "DPI"; // Try it e.g. with "NewPath | DPIs" (the latter doesn't exist)
 switch (isKeyValue($section, $key)) {
   case 1:
      msg "key only exists!"; break;
   case 2:
      msg "key and val exists!"; break;
  default:
      msg "no key exists!";
  } 

function isKeyValue($section, $key) {
    $isKey = regexmatches(getsectionlist($section), "^" . $key . "=.*?(?=\r?\n|$)");
    if ($isKey) {
        if(gettoken($isKey, 2, "=", "t", 2) != "") { return 2; }
        else {return 1; }
    }
    else {return 0;}

admin
Site Admin
Posts: 66094
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Test a section/key entry in .ini for existance

Post by admin »

No time to doc it, but in the next beta you can pass a return_on_error argument (if file, section, or key are invalid):

Code: Select all

text getkey("XXX", "General", 4:="FAIL"); //FAIL

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Test a section/key entry in .ini for existance

Post by PeterH »

Very helpful, thanks! :beer:

It seems to work for flags=0|1 (System|XY-algorythm), does it?
(Just to know...)

And I think you'll add it to help / getkey()? :mrgreen:

admin
Site Admin
Posts: 66094
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Test a section/key entry in .ini for existance

Post by admin »

Only for System.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Test a section/key entry in .ini for existance

Post by PeterH »

Oh - would have expected the opposite. :shock:

But not really a problem :D

admin
Site Admin
Posts: 66094
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Test a section/key entry in .ini for existance

Post by admin »

Will work for both in the next beta. :)

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Test a section/key entry in .ini for existance

Post by PeterH »

Even better without that restriction :tup:

Thanks again!

Post Reply