ExifTool Forum

ExifTool => Newbies => Topic started by: rparadinha on October 22, 2017, 06:59:03 AM

Title: Rename files to include create date
Post by: rparadinha on October 22, 2017, 06:59:03 AM
Hi everyone,

First let me thank you for this fantastic tool.
Let me explain my scenario.
I want to rename my all files (Images/Videos) to this format: YearMonthDay-HourMinuteSecond-Miliseconds-OriginalName
At same time I also intend to:

E.g. IMG_1827.jpg -> 20170923-235717-494-IMG_1827.jpg

Exploring my all files I have four cases:

1 - Image with $datatimeoriginal and milliseconds

# E.g. IMG_1827.jpg -> 20170923-235717-494-IMG_1827.jpg
# -Comment -> Original name file. E.g. 992501_502359019853043_44252601_n.jpg
# -UserComment -> Original name file. E.g. 992501_502359019853043_44252601_n.jpg

exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '$subsectimeoriginal' '-Comment<FileName' '-UserComment<FileName' '-filename<${datetimeoriginal}-${subsectimeoriginal;$_.=0 x(3-length)}-${FileName;}%-c' .


2 - Image with $datatimeoriginal and $datetimeoriginal not equal "0000:00:00 00:00:00" and without milliseconds

# E.g. DSC09780.JPG -> 20130713-201322-000-DSC09780.JPG
#
# -Comment -> Original name file. E.g. DSC09780.JPG
# -UserComment -> Original name file. E.g. DSC09780.JPG

exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original ${READ_EXTENSIONS} -if '$datetimeoriginal ne "0000:00:00 00:00:00" and not $subsectimeoriginal' '-filename<${datetimeoriginal}-000-${FileName;}%-c' .


3 - Image without $datatimeoriginal or $datetimeoriginal equal "0000:00:00 00:00:00" and with $createdate and $createdate not equal "0000:00:00 00:00:00"


# E.g. 100_0785.mov -> 20060101-025736-000-100_0785.mov
#
# -Comment -> Original name file. E.g. 100_0785.mov
# -UserComment -> Original name file. 100_0785.mov
#
# -DateTimeOriginal -> Get data from CreateDate. The standard EXIF date/time format is "YYYY:mm:dd HH:MM:SS". E.g 2013:07:13 12:30:45

exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and ($createdate) and ($createdate ne "0000:00:00 00:00:00")' '-Comment<FileName' '-UserComment<FileName' '-datetimeoriginal<createdate' '-filename<${createdate}-000-${FileName;}%-c' .


4 - Image without any time tag to get the information


# -FileName -> Get date for name of directory
# E.g. [2013-07-13] Test/992501_502359019853043_44252601_n.jpg -> [2013-07-13\] Test/20130713-000000-000-992501_502359019853043_44252601_n.jpg
#
# -Comment -> Original name file. E.g. 992501_502359019853043_44252601_n.jpg
# -UserComment -> Original name file. E.g. 992501_502359019853043_44252601_n.jpg
#
# -AllDates -> Get data for name of directory (HH:MM:SS are 0). The standard EXIF date/time format is "YYYY:mm:dd HH:MM:SS". E.g. 2013:07:13 00:00:00

exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' '-Comment<FileName' '-UserComment<FileName' '-AllDates<${directory;s/\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' '-filename<${directory;s/\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' .


My last command doesn't work as I expected. I can rename the file but the file is moved to a directory above and I can't update -Alldates field. :(

Can you help me?
Is there any way I can do what I want in fewer commands?

Thanks in advance


Title: Re: Rename files to include create date
Post by: Phil Harvey on October 22, 2017, 09:06:57 PM
I don't have time to read your whole post right now, but if you write a FileName with anything containing a slash ("/") then it will move the file to a new directory.  Take a look at what you are writing to FileName (write to TestName instead for this), and make sure you don't have any slashes.

- Phil
Title: Re: Rename files to include create date
Post by: StarGeek on October 22, 2017, 10:07:15 PM
If you list the Directory tag for that file exiftool -Directory, you will see that the full value of Directory is probably something like
./[2013-07-13] Test/20130713-000000-000-992501_502359019853043_44252601_n.jpg

Your regex strips the trailing characters you don't want, but doesn't deal with the leading characters ./, which probably is what moves it up a level.

One additional comment, Comment is not a tag you should rely on.  A lot of software out there will overwrite it without regards to what might have already been there.  Another option for the older filename would be PreservedFileName.
Title: Re: Rename files to include create date
Post by: rparadinha on October 23, 2017, 02:52:34 PM
Thanks for quick response.
So if i execute the command using -testname I get:

$ exiftool -m -p -a -r -d %Y%m%d-%H%M%S -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' -filename -AllDates -directory '-testname<${directory;s/\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
Ignored superfluous tag names or invalid options: -filename ...
'[2004-09-24] Convivio Recepção ao caloiro Pólo II/Convivio Recepção ao caloiro Pólo II 2004 (208).JPG' --> '[2004-09-24] Convivio Recepção ao caloiro Pólo II/20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG'
    0 image files updated
    1 image files unchanged

The file is with the correct name.
Print the -Directory field:

exiftool -m -a -r -d %Y%m%d-%H%M%S -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' -filename -AllDates -directory  \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
File Name                       : Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
Date/Time Original              : 0000:00:00 00:00:00
Create Date                     : 0000:00:00 00:00:00
Modify Date                     : 0000:00:00 00:00:00
Directory                       : [2004-09-24] Convivio Recepção ao caloiro Pólo II

And the -Directory field doesn't have "./".  :-\
Title: Re: Rename files to include create date
Post by: rparadinha on October 23, 2017, 03:23:30 PM
If I run the command directly to the file the file is correctly rename:

$ exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' '-Comment<FileName' '-UserComment<FileName' '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' '-filename<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
    1 image files updated

├── [2004-09-24]\ Convivio\ Recepça�\203o\ ao\ caloiro\ Po�\201lo\ II
│   └── 20040924-000000-000-Convivio\ Recepça�\203o\ ao\ caloiro\ Po�\201lo\ II\ 2004\ (208).JPG


I was runnnig a command in a bash script that's why the directory has a different value:

Directory                       : /Users/RobertoParadinha/Pictures/rp-photos-utils-test/[2004-09-24] Convivio Recepção ao caloiro Pólo II


So I change my regex to catch only what I want:

exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' '-PreservedFileName<FileName' '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' '-filename<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' ${DIR}

======== ./[2004-09-24] Convivio Recepção ao caloiro Pólo II/20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
File Name                       : 20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
Directory                       : ./[2004-09-24] Convivio Recepção ao caloiro Pólo II
Date/Time Original              : 0000:00:00 00:00:00
Create Date                     : 0000:00:00 00:00:00
Modify Date                     : 0000:00:00 00:00:00

But I can't update the -AllDates field. Do you know why?

Note: Thanks for PreservedFileName tip.
Title: Re: Rename files to include create date
Post by: rparadinha on October 23, 2017, 03:36:51 PM
Update:
So after I renamed the file I only tried to update the -AllDates fields:

$ exiftool -m -p -d %Y%m%d-%H%M%S -overwrite_original '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/20040924-000000-000-Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
Warning: Error opening file - %Y%m%d-%H%M%S
Error: File not found - %Y%m%d-%H%M%S
    1 image files updated
    1 files weren't updated due to errors

If I remove -d option the file is updated as exactly I want:

$ exiftool -m -p -overwrite_original '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/20040924-000000-000-Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
    1 image files updated

$ exiftool -m -a -r -filename -directory -AllDates \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/20040924-000000-000-Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
File Name                       : 20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
Directory                       : [2004-09-24] Convivio Recepção ao caloiro Pólo II
Date/Time Original              : 2004:09:24 00:00:00
Create Date                     : 2004:09:24 00:00:00
Modify Date                     : 2004:09:24 00:00:00


My question, why can't I do it in the same command, something like this:

exiftool -m -p -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' '-PreservedFileName<FileName' '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' '-filename<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' <file>

Title: Re: Rename files to include create date
Post by: StarGeek on October 23, 2017, 05:02:00 PM
Remove the -p option (https://exiftool.org/exiftool_pod.html#p-FMTFILE-or-STR--printFormat), it's messing with your command.  In the first example, -d becomes the argument to the -p option, leaving %Y%m%d-%H%M%S stranded and exiftool tries to treat it as a filename, leading to the Warning: Error opening file - %Y%m%d-%H%M%S error.

Maybe you wanted to use the -P option (https://exiftool.org/exiftool_pod.html#P--preserve) instead?
Title: Re: Rename files to include create date
Post by: rparadinha on October 23, 2017, 06:14:28 PM
Ah, right I want to use -P option, my mistake.  :D

I fixed my command and repeated it again but it still doesn't work.

$ exiftool -m -P -a -r -d %Y%m%d-%H%M%S -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' '-PreservedFileName<FileName' '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' '-filename<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' .
   18 directories scanned
    8 files failed condition
    1 image files updated

$ exiftool -m -a -r -filename -directory -AllDates -PreservedFileName \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/20040924-000000-000-Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
File Name                       : 20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
Directory                       : [2004-09-24] Convivio Recepção ao caloiro Pólo II
Date/Time Original              : 0000:00:00 00:00:00
Create Date                     : 0000:00:00 00:00:00
Modify Date                     : 0000:00:00 00:00:00
Preserved File Name             : Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
[\code]
Title: Re: Rename files to include create date
Post by: StarGeek on October 23, 2017, 06:26:43 PM
Now I'm starting to get confused, I could have sworn this was the thread in which Phil mentioned adding # (see here (https://exiftool.org/forum/index.php/topic,8649.msg44389.html#msg44389)).

You've added the -d option.  This reformats the DateTimeOriginal and CreateDate tags to 00000000-000000 and thus, will never equal 0000:00:00 00:00:00.  You need to add a hashtag to the end of the variables to get the original data before the printconvert

$datetimeoriginal# eq "0000:00:00 00:00:00"
$createdate# eq "0000:00:00 00:00:00"
Title: Re: Rename files to include create date
Post by: rparadinha on October 23, 2017, 07:20:06 PM
Sorry the long post, I'm try explain what I want:
In files that I can't extract any date from tags of the file I want to get the date from directory name and:
And if it's possible doing all steps in the same command.
So after doing more tests I found if I don't use -d option I get what I want:

Original file

$ exiftool -m -a -r -filename -directory -AllDates -PreservedFileName \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
File Name                       : Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
Directory                       : [2004-09-24] Convivio Recepção ao caloiro Pólo II
Date/Time Original              : 0000:00:00 00:00:00
Create Date                     : 0000:00:00 00:00:00
Modify Date                     : 0000:00:00 00:00:00


Command

$ exiftool -m -a -r -P -overwrite_original -if '(not $datetimeoriginal or $datetimeoriginal eq "0000:00:00 00:00:00") and (not $createdate or $createdate eq "0000:00:00 00:00:00")' '-PreservedFileName<FileName' '-AllDates<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}:${2}:${3} 00:00:00/}' '-filename<${directory;s/.*\[(\d{4})\-(\d{2})\-(\d{2}).*/${1}${2}${3}-000000-000/}-${FileName;}%-c' /Users/RobertoParadinha/Pictures/rp-photos-utils-test/
   18 directories scanned
    8 files failed condition
    1 image files updated


Final file

$ exiftool -m -a -r -filename -directory -AllDates -PreservedFileName \[2004-09-24\]\ Convivio\ Recepção\ ao\ caloiro\ Pólo\ II/20040924-000000-000-Convivio\ Recepção\ ao\ caloiro\ Pólo\ II\ 2004\ \(208\).JPG
File Name                       : 20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG
Directory                       : [2004-09-24] Convivio Recepção ao caloiro Pólo II
Date/Time Original              : 2004:09:24 00:00:00
Create Date                     : 2004:09:24 00:00:00
Modify Date                     : 2004:09:24 00:00:00
Preserved File Name             : Convivio Recepção ao caloiro Pólo II 2004 (208).JPG


I don't know why because I thought that I have to use -d option to set the format for date/time tag values. But in my case maybe it's not necessary.

My problems were:
Title: Re: Rename files to include create date
Post by: Phil Harvey on October 24, 2017, 07:37:03 AM
So your problems are solved, and the last command works.

Great!

The only thing I would suggest is to use "%f%-c.%e" instead of "${FileName;}%-c" when setting the file name because the way you have it the copy number would go after the file extension if there is a duplicate file name, but it should go before.

- Phil
Title: Re: Rename files to include create date
Post by: rparadinha on October 24, 2017, 02:23:25 PM
Thanks for your help!