Renaming "fallback"

Started by fracai, September 14, 2013, 02:07:39 AM

Previous topic - Next topic

fracai

I'm using a script to rename files from a source directory and move them to the destination library. The first pass tries to rename by DateTimeOriginal, and the second pass grabs anything that's still there and renames using FileModifyDate. Anything grabbed by the second pass presumably doesn't have the EXIF data like movies and PNGs.

#!/bin/sh

SOURCE="$1"
LIBRARY="$2"

FORMAT="$LIBRARY"/'%Y/%m/%d/%Y%m%d %H%M%S%%+2c.%%le'

if [ ! -d "$LIBRARY" ];
then
    echo "destination not available: $LIBRARY"
    exit
fi

exiftool -r '-FileName<DateTimeOriginal' -d "$FORMAT" "$SOURCE"
exiftool -ext MOV -ext JPG -ext PNG -ext GIF -r '-FileName<FileModifyDate' -d "$FORMAT" "$SOURCE"


Now though, I might want to re-organize the files by correcting modification dates or EXIF tags and then re-run the script to correct the sorting and filenames.

If I was using a single exiftool command, I could just specify the same directory for SOURCE and LIBRARY. ExifTool is smart enough to leave files alone if they name is already correct and won't end up renaming "file.jpg" to "file_01.jpg" just because the file itself already exists.

However, I'm using two passes, so the first would correctly rename according to the EXIF tag, but the second pass would rename everything according to the modification date. I could swap the order of the commands; rename by modification first, and then EXIF data; I could have the script move the Library to a temporary directory and use the two commands to move everything back again, but both of those options include a lot of redundant renaming.

Ideally I would run something like:
exiftool -ext MOV -ext JPG -ext PNG -ext GIF -r '-FileName<DateTimeOriginal -FileName<FileModifyDate' -d "$FORMAT" "$SOURCE"

The above would try to rename by DateTimeOriginal first and then fallback to FileModifyDate. Is such a thing possible?

fracai

I just noticed the '-if' argument. Is the solution to use both commands but include an '-if' test that only renames by FileModifyDate if the DateTimeOriginal does not exist?

fracai

So yeah, using the following works exactly how I'd like. I can specify the same directory for SOURCE and LIBRARY and files containing DateTimeOriginal are not renamed according to FileModifyDate. A single command would still be preferable as it'd be faster, but I seem to have a working solution.

#!/bin/sh

SOURCE="$1"
LIBRARY="$2"

FORMAT="$LIBRARY"/'%Y/%m/%d/%Y%m%d %H%M%S%%+2c.%%le'

if [ ! -d "$LIBRARY" ];
then
    echo "destination not available: $LIBRARY"
    exit
fi

exiftool -r '-FileName<DateTimeOriginal' -d "$FORMAT" "$SOURCE"
exiftool -ext MOV -ext JPG -ext PNG -ext GIF -r '-FileName<FileModifyDate' -if 'not $exif:DateTimeOriginal' -d "$FORMAT" "$SOURCE"

Phil Harvey

If it weren't for the -ext option that is only in your 2nd command, this would do exactly what you want:

exiftool -r '-FileName<FileModifyDate' '-FileName<DateTimeOriginal' -d "$FORMAT" "$SOURCE"

If DateTimeOriginal exists, this assignment will supercede the previous one.

- 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 ($).

fracai

I only have it on the second because it otherwise wouldn't act on those extensions. It wouldn't be a problem to have it on the first as well. Or is the flag interfering with the multiple rename commands?

Phil Harvey

The -ext won't interfere with anything -- it just restricts the processing to files with the specified extension(s).

- 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 ($).

fracai

I ended up using what you posted with the "-ext" as well. I needed those so that the second rename would apply to files that wouldn't otherwise be modified by ExifTool. Putting both commands together has worked perfectly.

exiftool -ext MOV -ext JPG -ext PNG -ext GIF -r '-FileName<FileModifyDate' '-FileName<DateTimeOriginal' -d "$FORMAT" "$SOURCE"

Thanks a lot.