Page 1 of 2

Test a section/key entry in .ini for existance

Posted: 02 Mar 2023 17:51
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".

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

Posted: 02 Mar 2023 18:55
by highend

Code: Select all

if !(regexmatches(getsectionlist("<section>"), "^<key>=")) { true if <key> doesn't exist... }

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

Posted: 02 Mar 2023 19:30
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.

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

Posted: 02 Mar 2023 21:24
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;
}

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

Posted: 02 Mar 2023 21:33
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;

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

Posted: 02 Mar 2023 22:52
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.

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

Posted: 02 Mar 2023 22:59
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...

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

Posted: 02 Mar 2023 23:24
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.

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

Posted: 03 Mar 2023 02:05
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;}

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

Posted: 03 Mar 2023 08:36
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

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

Posted: 03 Mar 2023 16:53
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:

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

Posted: 03 Mar 2023 17:23
by admin
Only for System.

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

Posted: 03 Mar 2023 22:38
by PeterH
Oh - would have expected the opposite. :shock:

But not really a problem :D

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

Posted: 04 Mar 2023 08:38
by admin
Will work for both in the next beta. :)

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

Posted: 04 Mar 2023 12:27
by PeterH
Even better without that restriction :tup:

Thanks again!