My quicksearch script is slow, please kindly comment

Discuss and share scripts and script files...
Post Reply
Akane2092
Posts: 7
Joined: 12 Jul 2025 03:42

My quicksearch script is slow, please kindly comment

Post by Akane2092 »

I have a script for user button as follows. Basically, it search for all files in a location that are 1) with a specific label and 2) with specific extensions. I think it work for me, but I find it a bit slow: usually takes 2-3 seconds to popup the menu after I click the button. Can you comment on the script and let me know how to improve. Thank you!

Code: Select all


perm $lbl;
	$location = "?:\";

	if(!<get drop>){sel;}
	
	$qs = "";
	$qs2 = quicksearch("lbl:#$lbl /d", $location);
	if ($qs2) {
		foreach($line, $qs2, "<crlf>"){
			$qs = $qs . $line . ";;;|";
			}
		$qs = $qs . "-|";
	}
	foreach($ext, "*.ppt*|*.xls*|*.doc*|*.pdf|*.eml") {
		$qs1 = "";
		$qs0 = quicksearch(":($ext) AND lbl:#$lbl /f", $location);
		if ($qs0) {
			foreach($line, $qs0,"<crlf>"){
				$qs1 =  $qs1 . $line . ";;;|";
				}
			}
		if ($qs1) {
		$qs = $qs . $qs1 . "-|";
		}
	}
	$qs = substr($qs,,-3);
	$path = popupmenu("$qs");
	end (!$path);		
	
	if(exists($path)==2) {
		if(!<get drop>) {
			$clickMenu = <<<>>>
Go to in Pane 1|focus p1; goto "$path";|left.ico|
Go to in Pane 2|focus p2; goto "$path";|right.ico|
Go to in New Tab|tab("new", "$path");|:newtab|
-
Remove Label|tag, "$path";|:wipe|
>>>;
			$sel = popupmenu($clickMenu, 6:="<crlf>", 7:="|");
			if ($sel) { load $sel, , "s"; }
		} 
		elseif(<get drop>) {
			$dropMenu = <<<>>>
Copy to "$path"|foreach($item, "<get drop | >"){ copyto "$path", "$item";};|c.ico|
Move to "$path"|foreach($item, "<get drop | >"){ moveto "$path", "$item";};|m.ico|
>>>;
			$sel = popupmenu($dropMenu, 6:="<crlf>", 7:="|");
			if ($sel) { 
				load $sel, , "s"; 
				#1001;
				focus pi;
				goto $path;
				#1001;
			}
		}
	}	elseif (exists($path)==1) {
		end (<get drop>);
		$clickMenu = <<<>>>
Open|open "$path";|:go|
-
Copy Here|copyto "<curpath>","$path";|c.ico|
Copy Here as <date yyyy-mm-dd-hh\hnn\m>__*|copyas "<date yyyy-mm-dd-hh\hnn\m>__*.?", "<curpath>","$path";|c.ico|
-
Go to in Pane 1|focus p1; goto "$path";|left.ico|
Go to in Pane 2|focus p2; goto "$path";|right.ico|
Go to in New Tab|tab("new", "$path");|:newtab|
-
Remove Label|tag, "$path";|:wipe|
>>>;
		$sel = popupmenu($clickMenu, 6:="<crlf>", 7:="|");
		if ($sel) { load $sel, , "s"; }

	}
	
	unset $lbl;

$lbl is set in the user button script field.

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

Re: My quicksearch script is slow, please kindly comment

Post by highend »

You don't want to loop over lines just to add a suffix:

No:

Code: Select all

		foreach($line, $qs2, "<crlf>"){
			$qs = $qs . $line . ";;;|";
			}
		$qs = $qs . "-|";
Instead:

Code: Select all

$qs = regexreplace($qs2 . <crlf>, <crlf>, ";;;|") . "-|";
Ofc the same is true for the second inner foreach^^

You don't want to iterate over each extensions in a loop to do a quicksearch for each of them and ofc you don't want to search the filesystem each time as well, but only tagged entries^^

No:

Code: Select all

    foreach($ext, "*.ppt*|*.xls*|*.doc*|*.pdf|*.eml") {
        $qs1 = "";
        $qs0 = quicksearch(":($ext) AND lbl:#$lbl /f", $location);
        if ($qs0) {
            foreach($line, $qs0,"<crlf>"){
                $qs1 =  $qs1 . $line . ";;;|";
                }
            }
        if ($qs1) {
        $qs = $qs . $qs1 . "-|";
        }
    }
But:

Code: Select all

    $qs0 = quicksearch("lbl:#$lbl /types=ppt*;xls*;doc*;pdf;eml", $location);
    $qs = regexreplace($qs0 . <crlf>, <crlf>, ";;;|") . "-|";
One of my scripts helped you out? Please donate via Paypal

Akane2092
Posts: 7
Joined: 12 Jul 2025 03:42

Re: My quicksearch script is slow, please kindly comment

Post by Akane2092 »

Thanks a lot, highend! Your suggestion is a game changer!

I modified according to your suggestion and now it loads much faster, almost instantaneously! But I still keep my extention foreach loop, because I want my result to group into menu sections by extention separated by -. My new version looks like this:

Code: Select all

	$qs = "";
	$qs2 = quicksearch("lbl:#$lbl /d", $location);
	if ($qs2) {$qs = regexreplace($qs2 . <crlf>, <crlf>, ";;;|") . "-|";}
	
	foreach($ext, "ppt*|xls*|doc*|pdf|eml") {
		$qs1 = "";
		$qs0 = quicksearch("lbl:#$lbl /types=$ext", $location);
		if ($qs0) { $qs1 = regexreplace($qs0 . <crlf>, <crlf>, ";;;|") ;}
		if ($qs1) { $qs = $qs . $qs1 . "-|";}
	}
	$qs = substr($qs,,-3); // this is to remove the final |-|
	$path = popupmenu("$qs");
	end (!$path);		

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

Re: My quicksearch script is slow, please kindly comment

Post by highend »

That's enough for the loop:

Code: Select all

    foreach($ext, "ppt*|xls*|doc*|pdf|eml") {
        $qsTmp = quicksearch("lbl:#$lbl /types=$ext", $location);
        if ($qsTmp) { $qs .= regexreplace($qsTmp . <crlf>, <crlf>, ";;;|") . "-|"; }
    }
One of my scripts helped you out? Please donate via Paypal

Akane2092
Posts: 7
Joined: 12 Jul 2025 03:42

Re: My quicksearch script is slow, please kindly comment

Post by Akane2092 »

Yes! You're right! It's very concise and elegant coding. Thank you again!

Post Reply