Page 1 of 1
Regex Rename with negative increment
Posted: 18 Jun 2012 04:44
by Steadyhands
Hi All,
I need to rename some images but with a negative increment in steps of -2 and be able to specify the starting number.
Image001 -> Image100
Image002 -> Image098
Image003 -> Image096
I figured a regex would be the only way to fo this in XY? Any suggestions, I can fugde a few simple regex's but this is beyond me.
Re: Regex Rename with negative increment
Posted: 18 Jun 2012 09:13
by highend
Renaming isn't (imho) the right way to do that. It can lead you into real trouble if you e.g. set the wrong starting value.
Code: Select all
Image01 - Image10
Image02 - Image09
Image03 - Image08
Image04 - Image07
Image05 - Image06
Image06 - Image05
Image07 - Image04
Image08 - Image03
Image09 - Image02
Image10 - Image01
In this example (left file names = original names | right ones = new names) all is well up to file "Image05" but when a script would reach Image06 it wouldn't be the old one, but the already renamed Image05 file.
You should rename them while copying one by one into a new directory.
You need an input() to set the starting number, the list of files via get() or folderreport() and a foreach loop with a regexreplace() and in the replacement expression a counter variable.
Something like:
Code: Select all
// Regexrename files
$input = input("Enter the starting number:");
$list = "Image001.jpg|Image002.jpg|Image003.jpg|Image004.jpg";
$output = "";
foreach($item, $list, "|") {
$format = format($input, "000");
$newName = regexreplace($item, "(.+?)(\d{3})", $1$format);
$output = $output . $newName . <crlf>;
$input = $input -2;
}
text $output;
It's just a demo, the copy command and the filelist shouldn't be a problem, right?
Re: Regex Rename with negative increment
Posted: 18 Jun 2012 10:34
by Stefan
Test this with some test files in an temp folder:
Code: Select all
// $c = 1; while($c<101){ $cc=format($c, "000"); new("Image$cc.ext"); $c++;}
$Items = get("SelectedItemsNames", "|");
foreach($Item, $Items) {
rename b, "$Item.tmp /e",, "<curpath>\$Item";
}
$Counter = 100;
foreach($Item, $Items) {
$Count = format($Counter, "000");
$Base = substr($Item, 0, strpos($Item,".", -1));
$Base = substr($Base, 0, -3);
$Exte = substr($Item, strpos($Item,".", -1));
msg "$Item<crlf>renamed to<crlf>$Base$Count$Exte",1;
rename b, "$Base$Count$Exte /e",, "<curpath>\$Item.tmp";
$Counter--;
$Counter--;
}
Re: Regex Rename with negative increment
Posted: 18 Jun 2012 20:38
by Steadyhands
Thanks guys, now to give those a try.