Renaming numbered files in OSX incrementally
May 24, 2007 10:31 AM Subscribe
Renaming numbered files in OSX incrementally
I have several hundred files that are alll named with the following convention: ABC5000.jpg. I'm looking for a way in OS X to rename groups of these files by increasing the numerical portion of the file name by one, ie, ABC5000.jpg becomes ABC5001.jpg.
I know that automator has a 'make sequential' feature in the rename filies applet, but that seems to only offer the option to add numbers, not change the ones that are already appended.
Thanks for any suggestions. I'm willing to buy software if needed.
I have several hundred files that are alll named with the following convention: ABC5000.jpg. I'm looking for a way in OS X to rename groups of these files by increasing the numerical portion of the file name by one, ie, ABC5000.jpg becomes ABC5001.jpg.
I know that automator has a 'make sequential' feature in the rename filies applet, but that seems to only offer the option to add numbers, not change the ones that are already appended.
Thanks for any suggestions. I'm willing to buy software if needed.
You can get Perl on OSX, right?
Here's a quick and dirty perl one-liner that should do it:
Just replace the "ABC*.jpg" part with some file wildcard that meets your needs (and replace the second .jpg if you have to also).
(You may want to test with some destroyable files first).
posted by jozxyqk at 10:56 AM on May 24, 2007 [1 favorite]
Here's a quick and dirty perl one-liner that should do it:
perl -e 'for (sort { $b ne $a } <ABC*.jpg>) { ($n) = /([^\.]+)/; $n++; rename $_,"$n.jpg"; }'
Just replace the "ABC*.jpg" part with some file wildcard that meets your needs (and replace the second .jpg if you have to also).
(You may want to test with some destroyable files first).
posted by jozxyqk at 10:56 AM on May 24, 2007 [1 favorite]
Response by poster: Corky Romano figured it out! The solution is to use automator's 'make sequential' feature and create new file names all together. After specifying the text prefix and a starting number the new sequential names will then have the corrected names.
Thanks for the other answers too.
posted by bcnarc at 12:09 PM on May 24, 2007
Thanks for the other answers too.
posted by bcnarc at 12:09 PM on May 24, 2007
jozxyqk should get a checkmark. His or her solution is lovely and geeky and one-line long and right and in perl and makes me all happy inside.
posted by The Bellman at 12:23 PM on May 24, 2007 [1 favorite]
posted by The Bellman at 12:23 PM on May 24, 2007 [1 favorite]
jozxyqk's solution is nice, but doesn't
initially set $n to something like "ABC5000," which isn't an integer? Would incrementing a string just return 1? Even if it captured just the numeric part of the file name, the leading 'ABC' isn't included in the renamed file.
(Also, dots don't need to be escaped inside a character class; [^.] should work fine.)
Perhaps this is better?
($n) = /^ABC(\d+)/; $n++; rename $_,"ABC$n.jpg";
posted by macrone at 3:31 PM on May 24, 2007
($n) = /([^\.]+)/; $n++; rename $_,"$n.jpg";
initially set $n to something like "ABC5000," which isn't an integer? Would incrementing a string just return 1? Even if it captured just the numeric part of the file name, the leading 'ABC' isn't included in the renamed file.
(Also, dots don't need to be escaped inside a character class; [^.] should work fine.)
Perhaps this is better?
posted by macrone at 3:31 PM on May 24, 2007
Aargh. Sorry for the entites in the rewritten code. Here it is again:
($n) = /^ABC(\d+)/; $n++; rename $_,"ABC$n.jpg";
posted by macrone at 3:32 PM on May 24, 2007
($n) = /^ABC(\d+)/; $n++; rename $_,"ABC$n.jpg";
posted by macrone at 3:32 PM on May 24, 2007
The freeware R-Name does this and many other file renaming tasks. You can preview the name changes before you commit. It's a vital tool in my toolbox.
posted by ardgedee at 3:36 PM on May 24, 2007
posted by ardgedee at 3:36 PM on May 24, 2007
macrone, perl's ++ is magic:
See here for more detail
posted by aneel at 11:49 PM on May 24, 2007
$ perl -e '$a = "ABC1234"; print ++$a . "\n"'
ABC1235
See here for more detail
posted by aneel at 11:49 PM on May 24, 2007
Rename4Mac is what I use:
http://www.power4mac.com/renamer/
posted by Ajit AP at 6:48 AM on May 25, 2007
http://www.power4mac.com/renamer/
posted by Ajit AP at 6:48 AM on May 25, 2007
This thread is closed to new comments.
posted by dance at 10:49 AM on May 24, 2007