Regex Rename with negative increment

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Steadyhands
Posts: 24
Joined: 13 Sep 2006 11:47
Location: Brisbane, Australia
Contact:

Regex Rename with negative increment

Post 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.

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

Re: Regex Rename with negative increment

Post 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?
One of my scripts helped you out? Please donate via Paypal

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Regex Rename with negative increment

Post 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--;
   }



Steadyhands
Posts: 24
Joined: 13 Sep 2006 11:47
Location: Brisbane, Australia
Contact:

Re: Regex Rename with negative increment

Post by Steadyhands »

Thanks guys, now to give those a try.

Post Reply