Renaming Nikon photograph files with Perl

I have owned a Nikon D3100 for a few years now. I generally have it set up to take nef/raw images and in camera processed jpg files so I end up with a load of files on my SD card called _DSCNNNN.NEF and _DSCNNNN.JPG. I sometimes edit the raw/nef files in Nikon capture NX and write a new JPG out as _DSCNNNN_copy.JPG and upload it to picasa or Nikon or google plus.

The other day the filename counter (NNNN a 4 digit number) cycled so my my photo files are nolonger unique and this caused me some obvious problems. I tried renaming the files in a batch process in capture NX but it only seems to work on nef or jpg files and cannot rename them consistently if you have both. Plus I edit nef files and rewrite as _DSCNNNN_copy.JPG and it certainly cannot cope with that.

The solution was to rename my existing files as PREFIX_FILENO_DATE_(copy).(NEF|JPG).

A quick and short script in Perl soon sorted this out for me. It is up on github at nefrename in case it helps anyone else out.

The marvelous Image::ExifTool reads the DateTimedOriginal from the exif data on the nef/jpg so it can be used in the filename (if that is what you want - see --date). I assume the NNNN in the filename is sequential and ordered in the way the photos were taken (which I think is safe until NNNN cycles and even then it hardly matters). It can copy the file (--copy) or rename it (--rename) and when copied it attempts to keep the modified and created times. You can specify a prefix for all filenames based on say location.

So:

_DSC1111.NEF => PREFIX_FILENO_YYYY_MM_DD.NEF
_DSC1111.JPG => PREFIX_FILENO_YYYY_MM_DD.JPG
_DSC1111_copy.JPG => PREFIX_FILENO_YYYY_MM_DD.JPG

I seriously doubt there are many Nikon camera owners who use Perl but just in case (afterall, it is not that difficult on Windows now with Strawberry Perl).