[SOLVED] How formatlist() filters item containing characters such as []

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Norn
Posts: 428
Joined: 24 Oct 2021 16:10

[SOLVED] How formatlist() filters item containing characters such as []

Post by Norn »

Use a list like this for testing:
list.png
list.png (6.33 KiB) Viewed 139 times
Items containing [] characters will not be filtered by formatlist():
result.png
result.png (5.72 KiB) Viewed 139 times

Code: Select all

    $items = get("ItemsPathNames", "<crlf>", "a");
    $paper = "";
    foreach ($item, $items, <crlf>) {
        if (!regexmatches($item, "^.*zip$|^.*rar$|^.*7z$")) {continue}
        $text = regexreplace($item, "(^.*).zip|.rar|.7z$", "$1");
        $text = regexreplace($text, "(^.*)\([0-9]{1,3}\)", "$1");
        $match = formatlist($items, "f", <crlf>, "$text<crlf>$text*");
        if (gettoken($match, 2, <crlf>)) {$paper .= $match . <crlf>}
    }
    $paper = formatlist($paper, "sd", <crlf>);
    paperfolder("Similar item name", $paper, <crlf>, "nl");
    //goto(lax('vi:$paper //"Similar item name"'));
    sortbylist $paper
Or is there any other simple way?
Last edited by Norn on 30 Apr 2024 10:21, edited 1 time in total.
Win10, Win11 @100% 2560x1440 22H2

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

Re: How formatlist() filters item containing characters such as []

Post by highend »

formatlist treats [ as the beginning of a range and # as a digit, so you need to escape them by surrounding them with []

Sorry but what is the exact expected output?
The result.png can't be it,
if (!regexmatches($item, "^.*zip$|^.*rar$|^.*7z$")) { continue; }
would filter everything out but:

Code: Select all

[2].zip
[2](1).zip
1.zip
And in general, you should do the prefiltering before the loop, e.g.:

Code: Select all

    $items = <get SelectedItemsPathNames>;
    $items = regexmatches($items, "^.*?(zip|rar|7z)$", <crlf>);
Btw:

Code: Select all

if (!regexmatches($item, "^.*zip$|^.*rar$|^.*7z$")) {continue} => "^.*\.(zip|rar|7z)$"
$text = regexreplace($item, "(^.*).zip|.rar|.7z$", "$1"); => Your dots before the extensions aren't really dots here, they are metachars
"(^.*)\([0-9]{1,3}\)" => Be very careful with such greedy expressions, the * would eat anything, make them lazy instead
One of my scripts helped you out? Please donate via Paypal

Norn
Posts: 428
Joined: 24 Oct 2021 16:10

Re: How formatlist() filters item containing characters such as []

Post by Norn »

formatlist treats [ as the beginning of a range and # as a digit, so you need to escape them by surrounding them with []
I have tried many times but no luck.
I haven't formally learned regular expressions yet, and you can see the mess of expressions in my code...
Win10, Win11 @100% 2560x1440 22H2

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

Re: How formatlist() filters item containing characters such as []

Post by highend »

$text = regexreplace($text, "(\[|#)", "[$1]");
One of my scripts helped you out? Please donate via Paypal

Norn
Posts: 428
Joined: 24 Oct 2021 16:10

Re: How formatlist() filters item containing characters such as []

Post by Norn »

Thanks!
I tested by renaming and it seems to be ineffective. :oops:
items.png
items.png (3.27 KiB) Viewed 101 times
Win10, Win11 @100% 2560x1440 22H2

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

Re: [SOLVED] How formatlist() filters item containing characters such as []

Post by highend »

What? This is for filtering items with formatlist(), not renaming them...

Again: For the files / folders you've listed, what should your script (the paperfolder) output / contain at the end?
One of my scripts helped you out? Please donate via Paypal

Post Reply