Rename files to include create date

Started by rparadinha, October 22, 2017, 06:59:03 AM

Previous topic - Next topic

rparadinha

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:

  • Insert in -Comment, -UserComment tags the original name of the file
  • Update any photo that doesn't have DateTimeOriginal using the CreateDate or getting the date from directory name

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



Phil Harvey

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

StarGeek

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.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

rparadinha

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 "./".  :-\

rparadinha

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.

rparadinha

#5
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>


StarGeek

Remove the -p option, 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 instead?
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

rparadinha

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]

StarGeek

Now I'm starting to get confused, I could have sworn this was the thread in which Phil mentioned adding # (see here).

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"
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

rparadinha

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:

  • Rename a file with this format YearMonthDay-HourMinuteSecond-Miliseconds-OriginalName

    Original 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
    Final name: 20040924-000000-000-Convivio Recepção ao caloiro Pólo II 2004 (208).JPG

  • Update -AllDates field using the date extract from directory name

    Initial tags:
    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

    Final tags:
    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

  • Update a field with the original name file. I'll use -PreservedFileName as you suggest.  ;)

    Preserved File Name             : Convivio Recepção ao caloiro Pólo II 2004 (208).JPG

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:

  • When I tested the command, I specified the file and for this case I don't have "./" in -directory tag. When I use the same command in root directory, the command rename de file, but because the name also have "/", like Phil said, the file is moved to root directory.
    So I fixed my regex and it stopped move the file.

  • The -AllDates was not update because I wrong used -p insteal -P and also because it's not necessary -d option.

Phil Harvey

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

rparadinha