Help needed with renaming files

Started by MnM, April 28, 2024, 10:56:45 PM

Previous topic - Next topic

MnM

Hi,

I have files in a folder. The files are from different months and years. I want to rename them so that 001, 002, 003, etc is appended. I have tested:


exiftool "-FileName<Datetimeoriginal" -d "%d-%m-%Y-%H-%M-%S-%%-.3nC.%%e" Z:/Incoming/PhotosTest/2024/*.jpg

and this does rename the files but only partially when compared to what I wanted to achieve.

What I want to achieve is for files from the same month and year to have the .3nC (001, 002, etc) but for files from a new month in the same year to start .3nC from 001 again.

So I will end up with all the January files having 001, 002, etc then all the February files with 001, 002, etc and so on for all the months.

When there is a new year then to rename the files as above again. So the end result will be:

For files from 2023 - January all will have 001, 002, 003
For files from 2023 - February all will have 001, 002, 003
and so on for the rest of the months in that year, then
For files from 2043 - January all will have 001, 002, 003
For files from 2043 - February all will have 001, 002, 003
and so on for subsequent months and years.

Is this even possible?

If it is will need some help to get the required command for that.

Thanks.

Phil Harvey

#1
This may be done in a 2-step process:

1. Rename the files with year-month only:

exiftool "-FileName<Datetimeoriginal" -d "%Y-%m-%%.3nc.%%e" -fileorder datetimeoriginal -ext jpg -r Z:/Incoming/PhotosTest

2. Rename them back to what you want, keeping the last 3 digits of the file name

exiftool "-FileName<Datetimeoriginal" -d "%d-%m-%Y-%H-%M-%S-%%-3f.%%e" -ext jpg -r Z:/Incoming/PhotosTest

The first command includes a -fileOrder option so the numbers are sequential in time.  I've also added -ext jpg and -r to process multiple folders at once.

However, I would really recommend switching to %Y-%m-%d-%H-%M-%S instead of %d-%m-%Y-%H-%M-%S because it sorts chronologically and it allows you to easily set any date/time tag from the file name if you ever wanted to do that.

- Phil

Edit: Changed %-.3f to %-3f
...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 ($).

MnM

#2
Hi thank you for looking at this.

I have tried the first command  - it renamed the files to this (as an example):

2022-10-001
2022-10-002
2024-04-001
2024-04-002

However when I have tried the second command it renamed them as:

23-10-2022-21-29-46-2022-10-
23-10-2022-21-29-49-2022-10-
24-04-2024-15-09-04-2024-04-
24-04-2024-15-09-22-2024-04-

I was expecting:
23-10-2022-21-29-46-001
23-10-2022-21-29-49-002
24-04-2024-15-09-04-001
24-04-2024-15-09-22-002

If you could have another look at what command is required in step 2 to get the above names will really appreciate it.
Another thing is that maybe in a certain month I will have over 1k of files - so I need to keep more than the last 3 digits of a file name.

Cheers.


Edit: All the files will be moved into year and month folders. I did take your suggestion into consideration (about swapping d, m, Y to Y, m, d but since all files will in their own month folder they will sort properly for me. Maybe this information will make things easier?
After I got the name right for the files I was going to run the below command to create the year and month  folders and to move the files into them. I prefer to run this step last as the command can create the folders and move the files into them in the area where I will keep these files.


exiftool -d "Z:/Incoming/PhotosTest/%Y/%m-%B-%Y" "-directory<CreateDate" "Z:/Incoming/PhotosTest"







StarGeek

I think it was supposed to be
%%-3f
instead of
%%-.3f
* 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).

MnM


MnM

One more issue.
Now when I run my last command to create year and month folders, and move the files into them it renames the files again (to the wrong names.

exiftool -d "Z:/Incoming/PhotosTest/%Y/%m-%B-%Y" "-directory<CreateDate" "Z:/Incoming/PhotosTest"

What should the command be now in order to create the folder, months and move files into them but keep the name as in step2?

Many thaks.

Phil Harvey

Quote from: StarGeek on April 29, 2024, 11:44:27 AMI think it was supposed to be
%%-3f
instead of
%%-.3f

Ooops.  Thanks.  I'll fix that in my original post.

Quote from: MnM on April 29, 2024, 11:58:08 AMOne more issue.
Now when I run my last command to create year and month folders, and move the files into them it renames the files again (to the wrong names.

exiftool -d "Z:/Incoming/PhotosTest/%Y/%m-%B-%Y" "-directory<CreateDate" "Z:/Incoming/PhotosTest"

This command will not rename the file.  (Did you perhaps write FileName instead of Directory?)

But you could move the file in the same step as the last rename by doing this:

exiftool "-FileName<Datetimeoriginal" -d "%%d%Y/%m-%B-%Y/%d-%m-%Y-%H-%M-%S-%%-3f.%%e" -ext jpg Z:/Incoming/PhotosTest

However, you should have told us that you were moving them like this, because then the whole process may be done in a single command instead of the 2-step process that I suggested.  Also, with the current process you'll run into trouble if files already exist in the subdirectories.  I'll leave it up to you to sort this out.

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

MnM

Hi Phil,

Yes my bad I should have explained the whole process from the beginning.

I am a beginner with this so I don't think I will ever get a chance to fix it all so I really need your experience :)

I will explain the process from the beginning and if you have a moment to have a look and suggest a better way I am eager to listen to and test your suggestions.

So every night I have my mobile phones move the pictures into a folder on my NAS. That folder (for this exercise) is Z:/Incoming/PhotosTest.

What I want to achieve is:

1. Rename the photo files as this example:
23-10-2022-21-29-46-001
23-10-2022-21-29-49-002
24-04-2024-15-09-04-001
24-04-2024-15-09-22-002
where the numbers at the end (001, 002) always starts from 001 for each month.

2. Next I want to create folders like Year and Month (month to be like 01-Jan-2024 for example) in the final path where the photos will "live" and from where they will be backed up to external USB and cloud.

The final folder structure will look like this:

2024
04-Apr-2024
24-04-2024-15-09-04-001
24-04-2024-15-09-22-002
10-Oct-2024
24-10-2024-15-09-04-001
24-10-2024-15-09-22-002

2023
01-Jan-2023
24-01-2024-15-09-04-001
24-01-2024-15-09-22-002
10-Dec-2024
24-12-2024-15-09-04-001
24-12-2024-15-09-22-002

3. Photos might be moved at a later date into already existing year folders and month sub-folders in the final destination directories.

The plan is to get the process right and then automate the hole thing via windows task scheduler batch files/commands.

Phil and/or StarGeek if you can suggest the best way to achieve this (based in your vast experience with exiftool) it will really help me a lot.
As a beginner I have no chance whatsoever to get it right myself and not to run into other issues (like the one you mentioned Phil where I will run into issues if files already exist in current sub-directories).

Many thanks again.

StarGeek

#8
A basic start would be something like this. Change Testname to Filename if the results look correct
exiftool -d "/Target/Path/%Y/%m-%b-%Y/%d-%m-%Y-%H-%M-%S-%%-1.3C.%%e" "-Testname<DateTimeOriginal" /path/to/files/

Links to read
Common Date Format Codes
Advanced features under the -w (-TextOut) option
* 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).

MnM

I have tried it but I get New file name is empty error:

Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA1.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA2.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA3.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA4.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA5.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA6.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA7.jpg
Warning: New file name is empty - Z:/Incoming/PhotosTest/HAHA8.jpg

StarGeek

Oops, I forgot to double the % in front of the %e extension variable
* 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).

MnM

Running this command now

exiftool -d "Z:/Incoming/PhotosTest/Target/%Y/%m-%b-%Y/%d-%m-%Y-%H-%M-%S-%-1.3C.%%e" "-Testname<DateTimeOriginal" Z:/Incoming/PhotosTest

But I get this output now

Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA1.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA2.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA3.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA4.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA5.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA6.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA7.jpg
Warning: File 'Z:/Incoming/PhotosTest/' already exists - Z:/Incoming/PhotosTest/HAHA8.jpg

StarGeek

Sorry, same problem.  When file name percent variables, %f, %d, %c, and %e are used in a DateFormat string, the percent sign needs to be doubled.  So same thing for the %c.
* 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).

MnM

#13
Many thanks for your help so far - moved things along a little bit :)

While folders look correct the file names don't - all files are named sequentially (only the 001 at the end) regardless of the month they belong to.

'Z:/Incoming/PhotosTest/HAHA.jpg' --> 'Z:/Incoming/PhotosTest/Target/2022/10-Oct-2022/22-10-2022-19-30-58-001.jpg'
'Z:/Incoming/PhotosTest/HAHA1.jpg' --> 'Z:/Incoming/PhotosTest/Target/2022/10-Oct-2022/23-10-2022-21-29-46-002.jpg'
'Z:/Incoming/PhotosTest/HAHA2.jpg' --> 'Z:/Incoming/PhotosTest/Target/2022/10-Oct-2022/23-10-2022-21-29-49-003.jpg'
'Z:/Incoming/PhotosTest/HAHA3.jpg' --> 'Z:/Incoming/PhotosTest/Target/2024/04-Apr-2024/24-04-2024-15-09-04-004.jpg'
'Z:/Incoming/PhotosTest/HAHA4.jpg' --> 'Z:/Incoming/PhotosTest/Target/2024/04-Apr-2024/24-04-2024-15-09-22-005.jpg'
'Z:/Incoming/PhotosTest/HAHA5.jpg' --> 'Z:/Incoming/PhotosTest/Target/2024/04-Apr-2024/25-04-2024-11-57-08-006.jpg'
'Z:/Incoming/PhotosTest/HAHA6.jpg' --> 'Z:/Incoming/PhotosTest/Target/2024/04-Apr-2024/25-04-2024-13-28-04-007.jpg'
'Z:/Incoming/PhotosTest/HAHA7.jpg' --> 'Z:/Incoming/PhotosTest/Target/2022/10-Oct-2022/27-10-2022-11-20-03-008.jpg'
'Z:/Incoming/PhotosTest/HAHA8.jpg' --> 'Z:/Incoming/PhotosTest/Target/2022/10-Oct-2022/27-10-2022-11-20-05-009.jpg'

Only files for the same month should be 001 , 002 sequentially. Files from different months should always start at 001 and continue from there.

Hopefully you have answer for this too :)

MnM

Any ideas? Can it be done in one step? Or do I need 2 steps as originally suggested?

Thanks for your help guys.

StarGeek

I had a misunderstanding of the - in the %C.  It restarts the numbering in the original directory structure, not the new directory structure.

Run the command once to get them all in the correct directories, and then run it again to get the correct numbers.

Example:
C:\>exiftool -G1 -a -s -ext jpg -r -p "$filepath" Y:\!temp\x\y
Y:/!temp/x/y/001.JPG
Y:/!temp/x/y/002.JPG
Y:/!temp/x/y/003.JPG
Y:/!temp/x/y/004.JPG
Y:/!temp/x/y/005.JPG
Y:/!temp/x/y/006.JPG
Y:/!temp/x/y/007.JPG
Y:/!temp/x/y/008.JPG
Y:/!temp/x/y/sub1/009.JPG
Y:/!temp/x/y/sub1/010.JPG
Y:/!temp/x/y/sub1/011.JPG
Y:/!temp/x/y/sub1/012.JPG
    2 directories scanned
   12 image files read

C:\>exiftool -d "Y:/!temp/x/y/%Y/%m-%b-%Y/%d-%m-%Y-%H-%M-%S-%%-1.3C.%%e" "-Filename<dto" Y:\!temp\x\y -r
    2 directories scanned
    2 directories created
   12 image files updated

C:\>exiftool -G1 -a -s -ext jpg -r -p "$filepath" Y:\!temp\x\y
Y:/!temp/x/y/2022/10-Oct-2022/22-10-2022-19-30-58-001.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-46-002.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-46-003.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-46-004.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-49-003.JPG
Y:/!temp/x/y/2022/10-Oct-2022/27-10-2022-11-20-03-008.JPG
Y:/!temp/x/y/2022/10-Oct-2022/27-10-2022-11-20-05-001.JPG
Y:/!temp/x/y/2024/04-Apr-2024/24-04-2024-15-09-04-004.JPG
Y:/!temp/x/y/2024/04-Apr-2024/24-04-2024-15-09-22-005.JPG
Y:/!temp/x/y/2024/04-Apr-2024/24-04-2024-15-10-04-002.JPG
Y:/!temp/x/y/2024/04-Apr-2024/25-04-2024-11-57-08-006.JPG
Y:/!temp/x/y/2024/04-Apr-2024/25-04-2024-13-28-04-007.JPG
    6 directories scanned
   12 image files read

C:\>exiftool -d "Y:/!temp/x/y/%Y/%m-%b-%Y/%d-%m-%Y-%H-%M-%S-%%-1.3C.%%e" "-Filename<dto" Y:\!temp\x\y -r
    6 directories scanned
    8 image files updated
    4 image files unchanged

C:\>exiftool -G1 -a -s -ext jpg -r -p "$filepath" Y:\!temp\x\y
Y:/!temp/x/y/2022/10-Oct-2022/22-10-2022-19-30-58-001.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-46-002.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-46-003.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-46-004.JPG
Y:/!temp/x/y/2022/10-Oct-2022/23-10-2022-21-29-49-005.JPG
Y:/!temp/x/y/2022/10-Oct-2022/27-10-2022-11-20-03-006.JPG
Y:/!temp/x/y/2022/10-Oct-2022/27-10-2022-11-20-05-007.JPG
Y:/!temp/x/y/2024/04-Apr-2024/24-04-2024-15-09-04-001.JPG
Y:/!temp/x/y/2024/04-Apr-2024/24-04-2024-15-09-22-002.JPG
Y:/!temp/x/y/2024/04-Apr-2024/24-04-2024-15-10-04-003.JPG
Y:/!temp/x/y/2024/04-Apr-2024/25-04-2024-11-57-08-004.JPG
Y:/!temp/x/y/2024/04-Apr-2024/25-04-2024-13-28-04-005.JPG
    6 directories scanned
   12 image files read

I didn't include the -fileorder datetimeoriginal that was in Phil's command, but it probably should be used.  If your camera fires in bursts, multiple shots/sec, then use SubSecDateTimeOriginal instead of DateTimeOriginal.
* 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).

Phil Harvey

Quote from: StarGeek on April 30, 2024, 07:19:40 PMI had a misunderstanding of the - in the %C.  It restarts the numbering in the original directory structure, not the new directory structure.

Right.  This could be confusing but I will try to make this more clear in the documentation.  It would be really tricky to implement the restarting of numbers in a new directory structure.

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