How to increment file number?

Started by theprof, October 19, 2024, 06:03:03 PM

Previous topic - Next topic

theprof

I want to rename all file names to be "4 Digit Year-2 Digit Month-2 Digit Day 2 digit 24 hour-2 digit minutes-2 digit seconds 2 digit File Duplicate Count.Extension". The date should come from the Create Date.

Some examples of the correct file name format:

2019-01-02 13:05:01 01.mp4
2019-01-02 13:05:01 02.mp4
2019-01-02 13:05:01 03.mp4
2019-01-03 09:50:29 01.mp4
2020-02-05 10:05:55 01.mp4


I tried it with this and FileSequence but it didn't work.

exiftool -api QuickTimeUTC -d "%Y-%m-%d %H-%M-%S %%e" "-filename<CreateDate" -r "$DIR"

StarGeek

You need to add the %c variable. See the -w (-TextOut) option for more details about the % variables.

From the docs "By default, the copy number is omitted from the first file of a given name, but this can be changed by adding a decimal point to the modifier."
%.

You want to pad to two characters, so you start with
%.2

"Also, %c and %C may be modified by 'n' to count using natural numbers starting from 1, instead of 0 "
%.2n

With the final result of
%.2nc

Because this will be added to a -d (-dateFormat) option format string, the percent sign has to be doubled, the same as the extension %e variable in you example (though you forgot the dot . before the extension)

Adding this to the command you tried
exiftool -api QuickTimeUTC -d "%Y-%m-%d %H-%M-%S %%.2nc.%%e" "-Testname<CreateDate" -r "$DIR"

Note I used Testname, which will be a dry run to show you what the results would be. If it is correct, change Testname into FileName

Also, the example names you give for the target filename format include a colon. If you're using Windows, this is an illegal character for a filename. I believe Mac/Linux will allow it but it might cause problems if you have to move the files to a Windows system.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

wywh

I have used the command below to rename images in macOS (in Windows use double quotes ").

It works otherwise well, but I have not found a way NOT to use file increment number 01 IF there are no date duplicates AND use file number 01 for the 1st one IF there are duplicates. So if there are duplicates I have to manually add 01 to the first image.

I use '-fileOrder5 FileName' so initial alphabetical file name order is used for the increments (unlike MacOS Extended, APFS file system otherwise feeds the files to exiftool in random order).

I have used 'ExifIFD:DateTimeOriginal' (the date/time when original image was created) as the golden standard for the image date. Recently I used only it but lately I have used '-AllDates' to cover also 'ExifIFD:CreateDate' (date/time that the digital file was created) and 'IFD0:ModifyDate' (supposed to be changed whenever the image is modified) if some app happens to need them. Usually they are the same anyway.

For movies I use 'QuickTime:CreateDate' ('Keys:CreationDate' might have the edit date if the movie is edited in a iOS/iPadOS mobile device but this might depend on the system version and the used workflow).

It would be possible to rename in subsecond accuracy but that bloats the file name too much for me. Quite rarely there are duplicate 1 second datetimes in my footage.

ls -l
a.jpg
b.jpg
c.jpg
d.jpg

exiftool -fileOrder5 FileName '-FileName<ExifIFD:DateTimeOriginal' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' .

ls -l                                                                                             
2024-0808-1839-45.jpg
2024-0808-1839-45_02.jpg
2024-0808-1839-45_03.jpg
2024-0808-1839-45_04.jpg

...so I manually have to edit that to:

2024-0808-1839-45_01.jpg
2024-0808-1839-45_02.jpg
2024-0808-1839-45_03.jpg
2024-0808-1839-45_04.jpg

- Matti

FrankB


wywh

#4
Quote from: FrankB on October 20, 2024, 05:24:20 AMHave a look at the uppercase C

I tried but the old problem persists unless I am again missing something obvious. I have asked the same thing and then the answer was "Can't be done".

The file order is random when '-fileOrder5 FileName' is not used in the 1st listing:

exiftool -a -G1 -s -DateTimeOriginal .                                                             
======== ./b.jpg
[ExifIFD]       DateTimeOriginal                : 2024:08:08 18:11:36
======== ./c.jpg
[ExifIFD]       DateTimeOriginal                : 2024:08:08 18:11:36
======== ./a.jpg
[ExifIFD]       DateTimeOriginal                : 2024:08:08 18:11:36
======== ./d.jpg
[ExifIFD]       DateTimeOriginal                : 2024:08:08 18:11:37

exiftool -fileOrder5 FileName '-FileName<ExifIFD:DateTimeOriginal' -d '%Y-%m%d-%H%M-%S%%+nC.%%e' .

2024-0808-1811-361.jpg
2024-0808-1811-362.jpg
2024-0808-1811-363.jpg
2024-0808-1811-374.jpg

So this works otherwise but the first duplicate increment _01 must be manually added:

exiftool -fileOrder5 FileName '-FileName<ExifIFD:DateTimeOriginal' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' .

2024-0808-1811-36.jpg
2024-0808-1811-36_02.jpg
2024-0808-1811-36_03.jpg
2024-0808-1811-37.jpg

- Matti

FrankB

This is how it works on Windows 11 and ExifTool 12.99
I dont use fileorder, but I can imagine that the default file order in Windows and Mac are not the same.

For more info on the Semicolon (%1:2C) see this post:
https://exiftool.org/forum/index.php?topic=16432.0

C:\RenameTest>exiftool -filename -Exif:DateTimeOriginal *.jpg
======== File1.jpg
File Name                       : File1.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== File2.jpg
File Name                       : File2.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== File3.jpg
File Name                       : File3.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== File4.jpg
File Name                       : File4.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== File5.jpg
File Name                       : File5.jpg
Date/Time Original              : 2024:09:04 01:02:03
    5 image files read

C:\RenameTest>exiftool -overwrite_original "-filename<${Exif:DateTimeOriginal} BlahBlah_%1:2C.%e" -d %Y%m%d *.jpg
    5 image files updated

C:\RenameTest>exiftool -filename -Exif:DateTimeOriginal *.jpg
======== 20240904 BlahBlah_01.jpg
File Name                       : 20240904 BlahBlah_01.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== 20240904 BlahBlah_02.jpg
File Name                       : 20240904 BlahBlah_02.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== 20240904 BlahBlah_03.jpg
File Name                       : 20240904 BlahBlah_03.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== 20240904 BlahBlah_04.jpg
File Name                       : 20240904 BlahBlah_04.jpg
Date/Time Original              : 2024:09:04 01:02:03
======== 20240904 BlahBlah_05.jpg
File Name                       : 20240904 BlahBlah_05.jpg
Date/Time Original              : 2024:09:04 01:02:03
    5 image files read

wywh

Quote from: FrankB on October 20, 2024, 06:31:29 AMThis is how it works on Windows 11 and ExifTool 12.99

Thanks, I'll study those options.

But that command adds increment _04 also to the non-date-duplicate d.jpg while I'd like the non-duplicates be renamed without any _01 suffix.

exiftool -a -G1 -s -fileOrder5 FileName -FileName -DateTimeOriginal .           
======== ./a.jpg
[System]        FileName                        : a.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./b.jpg
[System]        FileName                        : b.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./c.jpg
[System]        FileName                        : c.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./d.jpg
[System]        FileName                        : d.jpg
[ExifIFD]       DateTimeOriginal                : 2022:02:02 22:22:22

exiftool -fileOrder5 FileName '-FileName<${Exif:DateTimeOriginal BlahBlah_%1:2C.%e' -d %Y%m%d .

exiftool -a -G1 -s -fileOrder5 FileName -FileName -DateTimeOriginal .                         
======== ./20010101 BlahBlah_01.jpg
[System]        FileName                        : 20010101 BlahBlah_01.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./20010101 BlahBlah_02.jpg
[System]        FileName                        : 20010101 BlahBlah_02.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./20010101 BlahBlah_03.jpg
[System]        FileName                        : 20010101 BlahBlah_03.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./20220202 BlahBlah_04.jpg
[System]        FileName                        : 20220202 BlahBlah_04.jpg
[ExifIFD]       DateTimeOriginal                : 2022:02:02 22:22:22

Somehow GraphicConverter manages to do this:

2001-0101-1200-00_01.jpg
2001-0101-1200-00_02.jpg
2001-0101-1200-00_03.jpg
2022-0202-2222-22.jpg

- Matti

Phil Harvey

Hi Matti,

(we have sort of hijacked the original thread, but here goes anyway...)

Quote from: wywh on October 20, 2024, 03:26:01 AMI have not found a way NOT to use file increment number 01 IF there are no date duplicates AND use file number 01 for the 1st one IF there are duplicates.

I remember this coming up before, but can't think of a search in the forum that would find the original thread.

I suspect the answer is a 2-step process.

1. Rename all files as per your command:

exiftool -fileOrder5 FileName '-FileName<ExifIFD:DateTimeOriginal' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' .

2. Add "_01" to any file with a duplicate:

exiftool -filename=%-.3f_01.%e -srcfile %d%-.3f.%e *_02.*

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

wywh

Quote from: Phil Harvey on October 20, 2024, 08:18:10 AMI remember this coming up before, but can't think of a search in the forum that would find the original thread.

I suspect the answer is a 2-step process.

Thanks, that workaround works (I added Description to keep track which file is which):

exiftool -a -G1 -s -fileOrder5 FileName -FileName -Description -DateTimeOriginal .
======== ./a.jpg
[System]        FileName                        : a.jpg
[XMP-dc]        Description                     : a.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./b.jpg
[System]        FileName                        : b.jpg
[XMP-dc]        Description                     : b.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./c.jpg
[System]        FileName                        : c.jpg
[XMP-dc]        Description                     : c.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./d.jpg
[System]        FileName                        : d.jpg
[XMP-dc]        Description                     : d.jpg
[ExifIFD]       DateTimeOriginal                : 2022:02:02 22:22:22

exiftool -fileOrder5 FileName '-FileName<ExifIFD:DateTimeOriginal' -d '%Y-%m%d-%H%M-%S%%+2nc.%%e' .

exiftool -a -G1 -s -fileOrder5 FileName -FileName -Description -DateTimeOriginal .                
======== ./2001-0101-1200-00.jpg
[System]        FileName                        : 2001-0101-1200-00.jpg
[XMP-dc]        Description                     : a.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./2001-0101-1200-00_02.jpg
[System]        FileName                        : 2001-0101-1200-00_02.jpg
[XMP-dc]        Description                     : b.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./2001-0101-1200-00_03.jpg
[System]        FileName                        : 2001-0101-1200-00_03.jpg
[XMP-dc]        Description                     : c.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./2022-0202-2222-22.jpg
[System]        FileName                        : 2022-0202-2222-22.jpg
[XMP-dc]        Description                     : d.jpg
[ExifIFD]       DateTimeOriginal                : 2022:02:02 22:22:22

exiftool -FileName=%-.3f_01.%e -srcfile %d%-.3f.%e *_02.*

exiftool -a -G1 -s -fileOrder5 FileName -FileName -Description -DateTimeOriginal .
======== ./2001-0101-1200-00_01.jpg
[System]        FileName                        : 2001-0101-1200-00_01.jpg
[XMP-dc]        Description                     : a.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./2001-0101-1200-00_02.jpg
[System]        FileName                        : 2001-0101-1200-00_02.jpg
[XMP-dc]        Description                     : b.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./2001-0101-1200-00_03.jpg
[System]        FileName                        : 2001-0101-1200-00_03.jpg
[XMP-dc]        Description                     : c.jpg
[ExifIFD]       DateTimeOriginal                : 2001:01:01 12:00:00
======== ./2022-0202-2222-22.jpg
[System]        FileName                        : 2022-0202-2222-22.jpg
[XMP-dc]        Description                     : d.jpg
[ExifIFD]       DateTimeOriginal                : 2022:02:02 22:22:22

- Matti

FrankB

Quote from: wywh on October 20, 2024, 08:13:26 AMBut that command adds increment _04 also to the non-date-duplicate d.jpg while I'd like the non-duplicates be renamed without any _01 suffix.

OK. I had missed that requirement. Good that there's a workaround.