Geek Challenge: Mass rename

Here is the geek challenge this week... or month.. or whenever I get around to posting these challeneges... anyways back on topic. So you have a directory full of files that you need to copy into another directory but the problem is that you have namespace collision so you want to rename the files to be copied with a new name.

I run into this problem when I am pulling images off my digital camera. The camera assigns names like DSCFXXXX.JPG . This is all well and good until I want to store images that have the same name for a variety of reasons like they were on different flash cards and the camera number from 0001 for each card. So after unloading all of the images off the cards in seperate dirs I would like to combine them into the same dir. So dir1 has files DSCF0001.JPG through DSCF0150.JPG, and dir2 then has files DSCF0001.JPG through DSCF0120.JPG.

So what do I feel is a valid solution is, to create a script that will rename files in a given dir starting with a new number. For the example above I would go into dir2 and rename the files to start at DSCF0151.JPG and up. One catch is that what if I had the directories flipped and I wanted to rename the files in dir1 to start at DSCF0121.JPG and up... ah then one has to be careful since 0121 all ready exists in that directory...

Anyways have fun w/ the challenge. Please feel free to leave your solutions in the comments of this entry.

Remember that comments are free form but accept HTML so you might want to use the <pre> tags around the code.

Comments:

q&d:
cd /to && perl -e '($n = (<\*.JPG>)[-1]) =~ s/\\.JPG$//; grep { rename $_, ++$n . ".JPG" } </from/\*.JPG>'

Posted by guest on May 12, 2005 at 09:44 AM EDT #

iLife / iPhoto. Thought you were a Mac-boy?

Posted by guest on May 12, 2005 at 11:44 AM EDT #

Here is a link to my entry. It is in Python, and probably overkill.

http://www.bernerfam.com/erb/software/mv-and-renum

Posted by Edward Berner on May 12, 2005 at 12:17 PM EDT #

Or a more full featured perl script for doing mass renames. http://search.cpan.org/~pederst/rename-1.4/bin/rename.PL

Posted by Travis on May 12, 2005 at 03:59 PM EDT #

Thanks to everyone who replied or left a comment. I just finished using Edward's script and it works like a champ. I have not had a chance to play around w/ the perl mod that Travis suggested yet or the perl command that was left as well. For the one who pointed out that I am a Mac boy, that is true but I still like the old school stylings of managing my files myself.

Again, thanks to everyone that replied and commented.

dl

Posted by Dan Lacher on May 20, 2005 at 02:27 AM EDT #

It seems like a lot of these posts are focused on rename, but you specified that you wanted to do a copy. Someone mentioned to me the other day that pax(1) will do this on UNIX systems. So to copy and transform .JPG to .jpg:
$ pax -v -r -w -s '/JPG$/jpg/' . /tmp/new
/tmp/new/.
/tmp/new/./IMG_1437.jpg
/tmp/new/./IMG_1314.jpg
/tmp/new/./IMG_1315.jpg
/tmp/new/./IMG_1316.jpg
...
This was news to me, but it's another one for my toolkit!

Posted by Dan Price on May 22, 2005 at 11:24 PM EDT #

Post a Comment:
Comments are closed for this entry.
About

dlacher

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today