File Renaming Conundrum

Hello,

I’m trying to find a scrip that will batch rename files for me quickly but with a twist.

For example, I have files named xx123-987.jpg and xx123-987_2 (and _3 and _4 etc etc) that need to be renamed to 0001.jpg and 0001_2 (and _3 and _4 etc etc). I can put xx123-987 and 0001 in a CSV but I’m having trouble figuring out how to include the _2 and _3 and _4 (and more potentially) to be renamed as well.

I’ve looked into A Better Finder Rename to do this, which it can but it won’t do the _# because the _# won’t be listed in the CSV that is created from Filemaker Pro.

Is anyone able to offer up some advice, know of a script or feeling bored and what to challenge themself and write one for some mad props?

Hopefully someone can help,

Thanks for the help,

~eP

I don’t really have the time to write a full script for you, I did however put this together, if you put it in a function or wrap it in a for/foreach etc. i’m sure you can make use of it.

[php]// string to be renamed Note: we need to keep anything after the underscore!
$string = ‘xx123-987_2.jpg’;
print $string;

$pattern = ‘/(xx([0-9]+)-([0-9]+))/i’;
$replace = ‘0001’;
$newstring = preg_replace($pattern, $replace, $string);
print $newstring;

// outputs:
xx123-987_2.jpg // original filename.
0001_2.jpg // new filename
[/php]

Hope that helps you on your way,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service