ExifTool Forum

ExifTool => Newbies => Topic started by: JTS on May 23, 2020, 06:29:10 AM

Title: filtering the number from the filename
Post by: JTS on May 23, 2020, 06:29:10 AM
Hi,
I'm quite new with the tool, but already managed to rename an move all my photos from several drives to one location, and rename the files accoridng to the shuttercount.
I discovered that my old camera didn't have the shutter count to the file, so i want to use the file number as my guideline.
the file names are of these forms:
DSCN1234.JPG
DSCN1234-1.JPG
DSC_002-3.JPG

Is there a simple way, using regex to rename all the file to this convention:
E57_XXXX-Y.JPG
where E57_ is constant, XXXX is four digit representation of the "main" number (i.e. 1234 or 002 from the names above) and Y stands for the copy number, using the %%c operator.

THANKS!
Title: Re: filtering the number from the filename
Post by: StarGeek on May 23, 2020, 11:52:58 AM
To clarify, you are ignoring the -1 and the -3 from your above examples.  Also, you want the output to be, I assume, zero padded when it is less than 4 digits (0002 instead of 002).

Try this.  If it works as you like, change Testname to Filename
exiftool "-Testname<E57_${Filename;m/(\d\d+)/;$_=substr('0000'.$1,-4)}%-c.%e" /path/to/files

Here I'm grabbing numbers that have 2 or more digits, so as to avoid the trailing -1 and -3.  Then that gets padded out with leading 0s to make sure it's at least 4 digits.  If you want to increase of decrease the padding, change the -4 to the length of the number and add or remove 0s from the  0000 string.

The percent sign in %c doesn't need to be doubled unless it is used in the format string for the -d (dateFormat) option (https://exiftool.org/exiftool_pod.html#d-FMT--dateFormat) or if used in a Windows .bat file.

One thing you might consider doing first, if the filenames are important, would be to copy the original filenames into the file with
exiftool "-PreservedFileName<Filename" /path/to/files/
That way if you make a mistake, you can start over with
exiftool "-Filename<PreservedFileName" /path/to/files/
Title: Re: filtering the number from the filename
Post by: JTS on May 25, 2020, 03:59:55 AM
Works perfect!