Rename File Names

Discuss and share scripts and script files...
highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Rename File Names

Post by highend »

That's because it runs through all months for each file and therefore it would only rename files that use december as it's month...

Code: Select all

    setting "BackgroundFileOps", 0;
    $l_months = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
    foreach ($filename, "<get SelectedItemsNames |>") {
        $i=1;
        $newName = "";
        foreach ($month, $l_months) {
            if (regexmatches($filename, "\b$month[a-z]+\b")) {
                $newName = regexreplace(regexreplace($filename, "\b".$month."[a-z]+\b (\d\d*), (\d{4})", "$2.".$i.".$1"), "\b(\d)\b", "0$1");
                break;
            }
            $i++;
        }
        if ($newName) { renameitem($newName, $filename); }
    }
One of my scripts helped you out? Please donate via Paypal

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Rename File Names

Post by armsys »

highend wrote:That's because it runs through all months for each file and therefore it would only rename files that use december as it's month...
Finally, your script works brilliantly. Thanks a million. :appl:

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: Rename File Names

Post by FluxTorpedoe »

armsys wrote:The script fails to rename the selected filenames, except the first one.
Well, no real problem with the months per se, it’s because in your code, the $NewName variable that you added was set the first time:

Code: Select all

$NewName = regexreplace(regexreplace($filename,…
but then wasn’t reset before each next file.
So by using only $NewName in the regex you can just reset it with $NewName = $filename;.

Code: Select all

"Date converter"

 $l_months = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
 foreach ($filename, <get SelectedItemsNames |>) { $i=1;
  $NewName = $filename;
  foreach ($month, $l_months) {
    $NewName = regexreplace(regexreplace($NewName, "\b".$month."[a-z]+\b (\d\d*), (\d{4})", "$2.".$i++.".$1"), "\b(\d)\b", "0$1");
    }                               
    renameitem($NewName, $filename);
    }
Edit: Hehe, a bit late to post. Thanks highend for the “maintenance”. ;)
Last edited by FluxTorpedoe on 09 Nov 2015 11:20, edited 2 times in total.

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Rename File Names

Post by armsys »

Hi Flux,
Thanks you for your fast help.
Thanks you for speeding up my XY script learning.

Post Reply