Why does my ExifTool command move files instead of renaming them in place?

Started by micha_ontour, November 26, 2024, 09:31:51 AM

Previous topic - Next topic

micha_ontour

Hi everyone,

I'm using the following ExifTool command to rename my image files based on the name of their parent directory and append a 4-digit sequence number:

exiftool '-filename<${directory} (${filesequence;$_=sprintf("%04d",++$::count)}).%e' '-filemodifydate<datetimeoriginal' -r -ext arw DIR

However, instead of simply renaming the files in their current location, the command moves them to the parent directory. I expected the files to stay in place but with the updated names.

Could anyone explain why this happens and how I can modify my command to ensure the files are renamed in their original directory?

Thanks in advance!

StarGeek

Because you're using the Directory without any modifications.

If you run exiftool and just ask for the directory, you'll see something like this
C:\>exiftool -G1 -a -s -r -fileorder filename -Directory Y:\!temp\x\z
======== Y:/!temp/x/z/Test1.jpg
[System]        Directory                       : Y:/!temp/x/z
======== Y:/!temp/x/z/a/Test2.jpg
[System]        Directory                       : Y:/!temp/x/z/a
======== Y:/!temp/x/z/a/b/Test3.jpg
[System]        Directory                       : Y:/!temp/x/z/a/b
======== Y:/!temp/x/z/a/b/c/Test4.jpg
[System]        Directory                       : Y:/!temp/x/z/a/b/c
    4 directories scanned
    4 image files read

Note that the Directory tag does not have a trailing slash

Using "Test1.jpg" as an example, you are changing the name from
Y:/!temp/x/z/Test1.jpg
into
Y:/!temp/x/z (0001).jpg

Y:/!temp/x/z is the Directory, then the space, then your modified FileSequence.

The move to the parent directory would cascade all the way down
C:\>exiftool -r -fileorder Filename "-testname<${directory} (${filesequence;$_=sprintf('%04d',++$::count)}).%e" Y:\!temp\x\z
'Y:/!temp/x/z/Test1.jpg' --> 'Y:/!temp/x/z (0001).jpg'
'Y:/!temp/x/z/a/Test2.jpg' --> 'Y:/!temp/x/z/a (0002).jpg'
'Y:/!temp/x/z/a/b/Test3.jpg' --> 'Y:/!temp/x/z/a/b (0003).jpg'
'Y:/!temp/x/z/a/b/c/Test4.jpg' --> 'Y:/!temp/x/z/a/b/c (0004).jpg'
    4 directories scanned
    0 image files updated
    4 image files unchanged

Can you give an example of what you expect the file name to look like?
"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

greybeard

#2
This explains what happens when you update the filename or directory tags:

SG edit, changed to link
Writing "FileName" and "Directory" tags

StarGeek beat me to a response - I was going to ask for an example as well as its not clear what you want to happen.

Phil Harvey

It's always a good idea to write TestName first as StarGeek has done to see what will happen before actually writing FileName.

- 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

Quote from: greybeard on November 26, 2024, 09:59:19 AMI was going to ask for an example as well as its not clear what you want to happen.

Yeah, I couldn't tell if the desired results would be
Y:/!temp/x/z/z (0001).jpg
or
Y:/!temp/x/z/ (0001).jpg

The first seems more likely, but you never know.
"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

micha_ontour

Thanks for all of your quick help...

I'm on the road right now, so I'm not be able to test your advices...

Quote from: StarGeek on November 26, 2024, 09:55:32 AMCan you give an example of what you expect the file name to look like?

Quote from: StarGeek on November 26, 2024, 10:27:34 AMYeah, I couldn't tell if the desired results would be
Y:/!temp/x/z/z (0001).jpg
or
Y:/!temp/x/z/ (0001).jpg

It is actually the first version I would love to have:

Y:/temp/myFolder/myFolder (0001).jpg

Thank you again!

StarGeek

Try this
exiftool '-filename<%d%-1:D (${filesequence;$_=sprintf("%04d",++$::count)}).%e' '-filemodifydate<datetimeoriginal' -r -ext arw DIR

%d is the directory the file is in, including the trailing slash.
%D (capital D) is the directory the file is in, without the trailing slash. Then using the editing features of the percent variables (see the -w (-TextOut) option), inserting the -1: tells exiftool to only use the last directory the path.

But take note that when recursing into subdirectories, it will be the last subdirectory.

Using my original example
C:\>exiftool -r -fileorder Filename "-testname<%d%-1:D (${filesequence;$_=sprintf('%04d',++$::count)}).%e" Y:\!temp\x\z
'Y:/!temp/x/z/Test1.jpg' --> 'Y:/!temp/x/z/z (0001).jpg'
'Y:/!temp/x/z/a/Test2.jpg' --> 'Y:/!temp/x/z/a/a (0002).jpg'
'Y:/!temp/x/z/a/b/Test3.jpg' --> 'Y:/!temp/x/z/a/b/b (0003).jpg'
'Y:/!temp/x/z/a/b/c/Test4.jpg' --> 'Y:/!temp/x/z/a/b/c/c (0004).jpg'
    4 directories scanned
    0 image files updated
    4 image files unchanged
"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

micha_ontour

Quote from: StarGeek on November 26, 2024, 12:51:39 PMTry this
exiftool '-filename<%d%-1:D (${filesequence;$_=sprintf("%04d",++$::count)}).%e' '-filemodifydate<datetimeoriginal' -r -ext a

Thank you so much!

It's working like a charm.

Phil Harvey

I'm looking at this now and wondering why this was used:

  ${filesequence;$_=sprintf("%04d",++$::count)}

instead of the simpler:

  %1.4C

?

Of course, this means the "<" should be changed to "=" because no tags are then being copied, which makes the whole command more efficient.

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