Output new filename

Started by cmh, December 11, 2010, 06:21:13 AM

Previous topic - Next topic

cmh

First of all, thank-you for the wonderful tool Phil.

I have been searching for some time for an answer to what I thought would be a simple problem.  Maybe I have been using the wrong search terms or maybe its not as simple as I thought it would be.

Here is what I want to do:

I have one image (not a directory of images) and I want to RENAME the image with the date the file was created, this part is fairly simple using the examples given on the site (something like 'exiftool -d %Y%m%d_%H%M.%%e "-filename<CreateDate" image.JPG' will do it).  My problem comes because I would like exiftool to output the new filename so that I can pick it up easily in a script.  At the moment when I run exiftool it outputs something like "1 image files updated" but all I want it to output is the new filename.

Is this possible?  Do I need to explain this a little more?

Phil Harvey

Adding the -v option will give you a line like this in the console output:

'OLDNAME.jpg' --> 'NEWNAME.jpg'

which you should be able to parse to get what you want.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

cmh

Thank-you so much Phil it is much appreciated.

cmh

I guess this is slightly off topic but it might help others who are trying to do the same thing.

I thought I had this fixed but then I tried to generate a filename with a space in it and I am completely stuck again.

This is what I have:


#!/bin/bash
NEWPATH=(`exiftool -v '-FileName<${DateTimeOriginal}.%le' -d "%Y-%m-%d %Hh%Mm%S%%-c" "$1" | grep -o " --> '.*\.jpg'" | grep -o "\/.*\.jpg"`)
echo $NEWPATH
# find the new filename
PATHPARTS=(`echo $NEWPATH | tr '/' ' ' `)
FILENAME=${PATHPARTS[${#PATHPARTS[@]}-1]}
echo $FILENAME


You can run it something like this in a terminal window: bash script.sh /home/user/image.jpg

This bash script collects the filename it is passed ($1) and reformats the filename from DateTimeOriginal.  Using the verbose output (as suggested by Phil) I am trying to parse the exiftool output to collect the full path to the newly renamed file and just the filename of the newly renamed file.

The script as it stands states the filename is 2010-12-11 (or something similar) but I want "2010-12-11 09:45:00" (or whatever DateTimeOriginal states it should be).

If I change "%Y-%m-%d %Hh%Mm%S%%-c" (who the new filename is formatted) and remove the space character to make it something like "%Y-%m-%d_%Hh%Mm%S%%-c" it works fine.  Unfortunately the space character is vital for my needs.

Can anyone spot what I'm doing wrong?  I am new to bash scripting so it is probably all wrong!

cmh

It seems the solution is to set IFS.


IFS=$'\n'
NEWPATH=(`exiftool -v '-FileName<${DateTimeOriginal}.%le' -d "%Y-%m-%d %Hh%Mm%S%%-c" "$1" | grep -o " --> '.*\.jpg'" | grep -o "\/.*\.jpg"`)
# find the new filename
NEWFILENAME=$(basename $NEWPATH)
unset IFS