script to select files, folders in groups

Discuss and share scripts and script files...
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

script to select files, folders in groups

Post by tiago »

[split from User Functions Exchange]
Can I request a function here?

I'm having a hard time to code a script which could select 'files', 'folders', 'both' on groups of "x" with "y" skips
Like: select 'folders'
x = 3
y = 2

sel
file-not counted as skip
sel
sel
skip
skip
file
sel
sel
file
sel
skip
file
skip
Power-hungry user!!!

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

Re: User Functions Exchange

Post by highend »

I don't think that you'll learn a lot from this one if you don't show what you've got so far...

Edit: Small modification for the negation of the $types checking line -> less indentation
Renaming variables...

Code: Select all

    SelectItemsInGroups("b", 3, 2); // b = both, f = files, d = directories

function SelectItemsInGroups($types, $selSize, $skipSize) {
    $matches     = "";
    $curSelSize  = 0;
    $curSkipSize = 0;

    foreach($item, listpane()) {
        $type = exists($item);
        if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
        if ($curSelSize == $selSize) {
            if ($curSkipSize == $skipSize) {
                $curSelSize  = 1;
                $curSkipSize = 0;
                $matches     = $matches . $item . "<crlf>";
            } else { $curSkipSize++; }
        } else {
            $curSelSize++;
            $matches = $matches . $item . "<crlf>";
        }
    }
    selectitems $matches;
}
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

I tried. That's why I asked for the 'get' option with no great improvement on this issue. Nothing really useful.

Is it safe to mess with 'selectitems $matches;', pre-processing it via gettoken so a predefined max number of items is selected?

Thank you very much...

edit: not. I couldn't make it a proper function, then. :|
Power-hungry user!!!

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

Re: User Functions Exchange

Post by highend »

Add a parameter $limit to the function and do it in the foreach loop. Why should a script e.g. process 1000 items when your limit is e.g. 50. Or calculate a ratio $allItems / $limit and if it's low, use gettoken on $matches and if it's high, break the function once the limit is reached.

Without seeing what you coded, there is nothing to comment about...

As an advanced programmer you can learn a lot of other's code. But as a novice it isn't that helpful. In 90% of the cases you aren't able to recreate it after reading it in the first place...
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

wait...

is this wrong?

Code: Select all

    SelectItemsInGroups("d", 3, 2, 50); // b = both, f = files, d = directories

function SelectItemsInGroups($types, $selSize, $skipSize, $max) {
    $matches     = "";
    $curSelSize  = 0;
    $curSkipSize = 0;

    foreach($item, listpane()) {
        $type = exists($item);
        if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
        if ($curSelSize == $selSize) {
            if ($curSkipSize == $skipSize) {
                $curSelSize  = 1;
                $curSkipSize = 0;
                $matches     = $matches . $item . "<crlf>";
            } else { $curSkipSize++; }
        } else {
            $curSelSize++;
            $matches = $matches . $item . "<crlf>";
        }
    }
      $matches = gettoken($matches, $max, "<crlf>", , 1);
    selectitems $matches;
}
Initial test passed ok.
What's your opinion?
Power-hungry user!!!

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

highend wrote:Why should a script e.g. process 1000 items when your limit is e.g. 50.

Yes, I see your point now...
Last edited by tiago on 30 Apr 2016 20:04, edited 1 time in total.
Power-hungry user!!!

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

Re: User Functions Exchange

Post by highend »

Would work but as I already said, it's bad when you have a low limit and a large number of items...
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

What about this...?

Code: Select all

    SelectItemsInGroups("d", 3, 2, 20); // b = both, f = files, d = directories

function SelectItemsInGroups($types, $selSize, $skipSize, $max) {
    $matches     = "";
    $curSelSize  = 0;
    $curSkipSize = 0;

    foreach($item, listpane()) {
        $type = exists($item);
        if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
        if ($curSelSize == $selSize) {
            if ($curSkipSize == $skipSize) {
                $curSelSize  = 1;
                $curSkipSize = 0;

            $maxC++; if ($maxC == $max +1) { break; } elseif { }

                $matches     = $matches . $item . "<crlf>";
            } else { $curSkipSize++; }
        } else {
            $maxC++; if ($maxC == $max +1) { break; } elseif { }
            $curSelSize++;
            $matches = $matches . $item . "<crlf>";
        }
    }
//      $matches = gettoken($matches, $max, "<crlf>", , 1);
    selectitems $matches;
    #1012; // move into view
}
Power-hungry user!!!

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

Re: User Functions Exchange

Post by highend »

Too complicated. A single check on top of the foreach loop would be enough. And you don't need to add the +1 at that place. The incrementation must (ofc) still take place in their current places. If there is no elseif(), don't write it^^. Do not write multiple statements in one line. Allowed? Yes. Readable? Barely. Multiple assignments, ok, but not a combination with anything else.
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

I'm breaking the script with attempts of single check.
Power-hungry user!!!

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

SammaySarkar wrote:allstrpos(haystack, needle, start, matchcase, rel) : Returns all positions of a substring, separated by pipe.

The Return is similar to regular strpos(), but for negative start position. Details in function comment.
...
Hiya, SammaySarkar!
Can you please provide a version in which whole words can be used in haystacks? Having much trouble on this. I'd swear strpos would do it but unfortunately it doesnt.

@highend:
What would it be your solution for SelectItemsInGroups then? Couldn't simplify it per your instructions. Sorry...
Power-hungry user!!!

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

Re: User Functions Exchange

Post by highend »

Can you please provide a version in which whole words can be used in haystacks? Having much trouble on this. I'd swear strpos would do it but unfortunately it doesnt.
Give a real word example. Regexmatches with word bounderies sounds like a reasonable solution.
Couldn't simplify it per your instructions

Code: Select all

    SelectItemsInGroups("b", 3, 2, 10); // b = both, f = files, d = directories

function SelectItemsInGroups($types, $selSize, $skipSize, $limit=1) {
    $matches     = "";
    $curSelSize  = 0;
    $curSkipSize = 0;
    $curCount    = 0;

    foreach($item, listpane()) {
        if ($curCount == $limit) { break; }
        $type = exists($item);
        if !($types == "b" || ($types == "f" && $type == 1) || ($types == "d" && $type == 2)) { continue; }
        if ($curSelSize == $selSize) {
            if ($curSkipSize == $skipSize) {
                $curCount++;
                $curSelSize  = 1;
                $curSkipSize = 0;
                $matches = $matches . $item . "<crlf>";
            } else { $curSkipSize++; }
        } else {
            $curCount++;
            $curSelSize++;
            $matches = $matches . $item . "<crlf>";
        }
    }
    selectitems replace($matches, "<crlf>", "|");
}

One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

Thank you for the code.

Pseudo:

Code: Select all


   $blacklist = "que,de";

   $source = "um,a,para,que,quando,de,";

   foreach($token, "$source", ",") {
   $check = strpos("$blacklist", "$token");
   if ($check != "-1") { continue; }
                                   }
Power-hungry user!!!

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

Re: User Functions Exchange

Post by highend »

?

What has allstrpos to do with a blacklist?

I don't want pseudo code. Describe in plain, easy words what you actually want to do, the input AND the expected output!
One of my scripts helped you out? Please donate via Paypal

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: User Functions Exchange

Post by tiago »

Well.
It's pseudo because strpos nor any known function I'm aware of can do it. strpos being the closer I get to this.
Per the request I'd like to have the ability to use words in the haystack which strpos as it is won't do.
Power-hungry user!!!

Post Reply