Renaming files according to current directory structure

Started by ExifFool, October 10, 2012, 02:56:51 PM

Previous topic - Next topic

ExifFool

Hello folks, a newbie question here:

I am moving my photos to a new directory and at the same time renaming them. I am using this line of code:

exiftool -r   '-FileName<CreateDate' -d %Y/%y%m/%y%m%d_%H%M-%S_%%f.%%e  -P /volume1/photos

I run this in the directory I want the files to be moved to, and it works fine with my newer photos. Tey are moved to the right directory and renamed according to creation date in the Exif. Modification date is not changed which is good too (-P).

My older photos however, contain files with wrong date/time settings, though they are currently ordered in the right yy/yy_mm/ folders. Is there a way to rename the files according to the existing folder-name, and then move them? Preferably I would adjust the exif-data too to at least the right month.


Phil Harvey

You could do something like this:

exiftool -r '-filename<DSTDIR/20%2d%2.3d/%2d%2.3d01_0000-00_%f.%e' SRCDIR

which will rename the files (assuming all pictures are year 2000 or later -- if there are some in the 1900's then you'll have to use 2 commands and add a -if option).

Adding the partial date to the metadata can be done after renaming the files with:

exiftool -r '-createdate<20$filename' if 'not $createdate' DSTDIR

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

ExifFool

I noticed I forgot to mention one thing: most are dated in the exif right, some are not. So how do I first check which folders contain mis-dated files, and then rename them?

I thought to do this by running exiftool -r  -o  '-FileName<CreateDate' -d %Y/%y%m/%y%m%d_%H%M-%S_%%f.%%e  -P /volume1/NAS/Photos/2005 in a test directory where -o would copy the files instead of move.
However, I get the error
Quote-FileName<CreateDate already exist

This might be because of something related to the syntax which requires a directory to be defined together with -o?

QuoteA file name or directory specified via the FileName or Directory tag takes precedence over that specified by the -o option, so these three commands all have the same effect: 'tmp/new.jpg' is created without changing 'image.jpg'. Note that in the first command, the trailing '/' on 'tmp/' is necessary if the 'tmp' directory doesn't already exist, otherwise 'tmp' would be taken as a file name and 'new.jpg' would be created in the current directory. As illustrated in example 4 above, the file is rewritten instead of simply being renamed when the '-o' option is used.

If I understand this right, I have to specify a directory with -o, but the files will be written according to the Filename switch as this takes precedence over -o. At least is seems to work like that when testing.

Phil Harvey

Quote from: ExifFool on October 17, 2012, 02:51:10 PM
I noticed I forgot to mention one thing: most are dated in the exif right, some are not. So how do I first check which folders contain mis-dated files, and then rename them?

I'm a bit confused about exactly what you want to do now.  The EXIF date disagrees with the folder name?  It may be difficult to automate a check for this.

QuoteI thought to do this by running exiftool -r  -o  '-FileName<CreateDate' -d %Y/%y%m/%y%m%d_%H%M-%S_%%f.%%e  -P /volume1/NAS/Photos/2005 in a test directory where -o would copy the files instead of move.

The -o option takes one argument which is the output directory or file name.  In your command, exiftool will write the output to a file called "-FileName<CreateDate", which isn't 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 ($).

ExifFool

Thank you for your fast reply again!
Yes, Exiftool created this Filename<CreateDate file with this command...  :P

Now it is running fine with a folder defined.

I realized that what I want cannot be automated fully, so I have to figure out how I work around it. Maybe first rename based on current folder, then move files based on exif. I will then see what files did not have the right exif date (or non at all) and can adjust based on file name.
Well, have to think how I work around it...

Phil Harvey

I just thought of something:  Use ExifTool to construct the directory name from the EXIF date/time, then compare this to the actual directory in a -if statement:

exiftool -d "DIR/%Y/%y%m" -if '$createdate ne $directory' -r DIR ...

This command should process only files with an incorrect directory (provided you get the -d option argument exactly correct).  The -if condition will return false if CreateDate doesn't exist or if it is the same as Directory.

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

ebony111

Phil - Thanks for putting such a great tool together.  I am trying to do something similar, but different.  I have tried a bunch of different combinations, but can't seem to figure out the code.  I am a super newbie, so sorry for the basic question.

I have a huge library that was exported from iphoto a few years ago and the exif date fields were all change to show the export date.  disaster.  However, I have sense organized the files into the following structure user/images/yyyy/mm/dd, and i would like to run a command to change the exif dates to correspond to the file structure.  Is that possible.  I have successfully changed the exif dates using the alldates function (see below), but I can't seem to do it using the file structure. 

Exiftool -AllDates='2002:06:.21 12:00:00' -overwrite_original /Users/Josh/images.  Obviously this function changes all the files in images to the same dates, which is not what i want.

Any help would be much appreciated.

Phil Harvey

Quote from: ebony111 on November 11, 2012, 01:19:30 AM
I have sense organized the files into the following structure user/images/yyyy/mm/dd, and i would like to run a command to change the exif dates to correspond to the file structure.

The command is exiftool "-alldates<$directory 12:00:00" DIR

(use double quotes as above in Windows, or single quotes in Mac/Linux)

ExifTool uses some magic to pull any numbers that look like a date/time out of whatever string you assign to a date/time tag.

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

ebony111

Quote
The command is exiftool "-alldates<$directory 12:00:00" DIR

(use double quotes as above in Windows, or single quotes in Mac/Linux)

ExifTool uses some magic to pull any numbers that look like a date/time out of whatever string you assign to a date/time tag.

Wow - magic indeed!  Works great and thank you for the quick response.  One more question, do I have to specify a time, or can I use the existing exif time in the date fields?  thanks

Phil Harvey

Using the existing time is possible, but tricky because you will have to remove the date part first.  This can be done with a Composite user-defined tag and this conversion:

    ValueConv => '$val =~ s/.* //; $val',

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