Page 1 of 1

rename files

Posted: 16 Sep 2021 14:47
by rosso
Hy,

i'm new in the xyplorer community and i have already my first problem:

I have a load of files wich are named like abcd_1.pdf, efgh_2.pdf, ijkl_3.pdf and so on but i need them to be like abcd_001.pdf, efgh_002.pdf, ijkl_003.pdf,......
Is there a quick way to do that?

thanks in advance

Re: rename files

Posted: 16 Sep 2021 15:06
by highend
Only doable via scripting (as long as the number part can be greater than 9)...

Select all those items and execute this:

Code: Select all

    $items = <get SelectedItemsPathNames>;
    end (!$items), "No item(s) selected, aborted!";

    $src = "";
    $dst = "";
    foreach($item, $items, <crlf>, "e") {
        if (exists($item) == 2) { continue; }
        $base  = gpc($item, "base");
        $ext   = gpc($item, "ext");
        $match = trim(regexmatches($base, "_\d+?$"), "_", "L");
        if ($match) {
            $src .= $item . <crlf>;
            $base = regexreplace($base, "^(.+?)_(\d+?)$", "$1");
            $dst .= $base . "_" . format($match, "000") . ".$ext" . <crlf>;
        }
    }
    if ($dst) {
        rename "l", trim($dst, <crlf>), "p", trim($src, <crlf>), 64;
    }