rename folder search result ...

Discuss and share scripts and script files...
Post Reply
drjs5x
Posts: 152
Joined: 18 Nov 2015 18:12
Location: Turkey

rename folder search result ...

Post by drjs5x »

testfolder (my root folder)
test1folder 2xls 4doc files
test2folder 7xls files
test3dolder 5doc files
test4folder
test5folder 3 doc files

script find xls or doc count rename folder rename result
test1folder_ 2xls_ 4doc
test2folder_7xls
test3dolder_5doc
test4folder (no rename)
test5folder_3doc
too diffucult script , who can ?
please help
Sincerely
newbie or not...........

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

Re: rename folder search result ...

Post by highend »

foreach loop
listfolder()
gettoken() with "count"
rename()

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

drjs5x
Posts: 152
Joined: 18 Nov 2015 18:12
Location: Turkey

Re: rename folder search result ...

Post by drjs5x »

im newbie but u re prof.
thanks for help dear.
i will try ....
:oops:
newbie or not...........

shpkong
Posts: 6
Joined: 14 Jan 2017 04:20
Location: Myanmar

Re: rename folder search result ...

Post by shpkong »

Hi drjs5x, I 'd like to help. :)
foreach loop
listfolder()
gettoken() with "count"
rename()

Have fun
Thank you very much for your guidance, I understood foreach loop and listfolder(), but I can not figure out how to gettoken() with "count". I do not know how to get differet exts from the string and count them, a K.M.P.?

Here is my way to figure it out. The files listed in a folder maybe in a random order, so I first go to that folder and sort all the files by Exts and then select all of them. It seems that after that sort and select the job can be done much easier. :D

Code: Select all

$sels = get('SelectedItemsPathNames', '|', 'a');
 $res = 0;
 foreach($i, $sels)
 {
     if (exists($i) != 2) {continue;}
     $res = 1;
     goto $i;
     sortby "ext";
     sel f;
     $sels2 = get('SelectedItemsNames', '|', 'a');
     $before = "....";
     $result = "";
     $count = 0;
     $cur = "";
     foreach($j, $sels2, , "No file.")
     {
	 if(gettoken($j, "count", ".") > 1)
	 {
	     $cur = gettoken($j, -1, ".");
	 }
         if(($cur != $before) && ($before != "...."))
	 {
             $result = $result.$count.$before."_";
	     $before = $cur;
	     $count = 0;
	 } elseif ($before == "....")
	 {
	     $before = $cur;
	 }
	 $count++;
     }
     if($count != 0)
     {
         $result = $result.$count.$cur;
         renameitem("*_$result", $i);
     }
 }
 if ($res)
 {
    goto "<curpath>\..";
    msg "Job done.";
 }
 else 
 {
    msg "No folder.";
 }
Last edited by shpkong on 15 Jan 2017 10:58, edited 3 times in total.

shpkong
Posts: 6
Joined: 14 Jan 2017 04:20
Location: Myanmar

Re: rename folder search result ...

Post by shpkong »

This script support multi folders.

Just select some folders and the script will do all the job for you. :lol:

Maybe not fast enough...

shpkong
Posts: 6
Joined: 14 Jan 2017 04:20
Location: Myanmar

Re: rename folder search result ...

Post by shpkong »

Hi, drjs5x

You only want to count .xls and .doc, right?

Maybe I am over-anxious...

My slow script can support any file type under a folder.

If only xls or doc, I think gettoken() with "count" can do the job.

shpkong
Posts: 6
Joined: 14 Jan 2017 04:20
Location: Myanmar

Re: rename folder search result ...

Post by shpkong »

Code: Select all

$sels = get('SelectedItemsPathNames', '|', 'a');
 $res = 0;
 foreach($i, $sels)
 {
     if (exists($i) != 2) {continue;}
     $res = 1;
     $list = listfolder($i);
     $a = 0;
     $b = 0;
     if(gettoken($list, "count", ".xls") > 1)
     {
         $a = 1;
         $count_xls = gettoken($list, "count", ".xls") - 1;
	 $xls = $count_xls."xls";
     }
     if(gettoken($list, "count", ".doc") > 1)
     {
         $b = 1;
         $count_doc = gettoken($list, "count", ".doc") - 1;
	 $doc = $count_doc."doc";
     }
     if($a && !$b) {renameitem("*_$xls", $i);}
     if(!$a && $b) {renameitem("*_$doc", $i);}
     if($a && $b) {$result = $xls."_".$doc;renameitem("*_$result", $i);}
 }
 if ($res)
 {
    msg "Job done.";
 }
 else 
 {
    msg "No folder.";
 }
Note: This script only count .xls and .doc. If you want to count all the file types inside a folder, get my previous one.

Note2: If some file names under the selected folder also contain .xls or .doc, this script return wrong answer.

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

Re: rename folder search result ...

Post by highend »

The thing is, if you want it to work on any subfolder structure you should rename the deepest nested ones first...

So, this should be rather fast (about half a second on a structure with 2000 files in 612 folders with about 20 doc
and 5 xls files). Test conducted with a ram disk to rule out slow hdd drives if you don't use ssds...

Code: Select all

    $exts   = "*.doc;*.xls";
    $result = quicksearch($exts, get("SelectedItemsPathNames", ";"));
    $sorted = FolderDepthSort($result);

    $renamed = "";
    while ($sorted) {
        $line = gettoken($sorted, 1, <crlf>);
        $ext  = gpc($line, "ext");
        $path = gpc($line, "path");

        $pattern   = regexreplace("$path", "(#|\[)", "[$1]");
        $samePaths = formatlist($sorted, "ef", <crlf>, "$pattern\*");

        $docCnt = gettoken(regexmatches($samePaths, "\.doc$"), "count", "|");
        $xlsCnt = gettoken(regexmatches($samePaths, "\.xls$"), "count", "|");

        $sorted = formatlist($sorted, "ef", <crlf>, "!" . regexreplace($samePaths, "(#|\[)", "[$1]"));
        $allPrefix = (($docCnt) ? "_$docCnt" . "doc" : "") . (($xlsCnt) ? "_$xlsCnt" . "xls" : "");
        $renamed = $renamed . "From: $path<crlf>To  : $path$allPrefix" . <crlf 2>;
        rename "b", "*$allPrefix", , $path;
    }
    if ($renamed) { text "Rename operation(s) executed:<crlf 2>$renamed"; }
    else { msg "No folders with $exts found..."; }

function FolderDepthSort($list, $inDelim="<crlf>", $outDelim="<crlf>") {
    $result = "";
    foreach($item, $list, $inDelim, "e") {
        $count = gettoken($item, "count", "\");
        if ($count >= $lastCount) { $result = $item . $outDelim . $result; }
        else { $result = $result . $outDelim . $item; }
        $lastCount = $count;
    }
    return formatlist($result, "e", $outDelim);
}
One of my scripts helped you out? Please donate via Paypal

Post Reply