Page 2 of 2

Re: Rename File Names

Posted: 09 Nov 2015 10:38
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); }
    }

Re: Rename File Names

Posted: 09 Nov 2015 11:14
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:

Re: Rename File Names

Posted: 09 Nov 2015 11:15
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”. ;)

Re: Rename File Names

Posted: 09 Nov 2015 11:18
by armsys
Hi Flux,
Thanks you for your fast help.
Thanks you for speeding up my XY script learning.