Author Topic: filtering the number from the filename  (Read 90 times)

JTS

  • Newbie
  • *
  • Posts: 2
filtering the number from the filename
« 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!

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 3684
Re: filtering the number from the filename
« Reply #1 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 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/
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

JTS

  • Newbie
  • *
  • Posts: 2
Re: filtering the number from the filename
« Reply #2 on: May 25, 2020, 03:59:55 AM »
Works perfect!