2

I have the following script that works just fine except that it can't handle duplicate file names. How can I simply amend it so that if a picture with the same name is found it simply adds an increment at the end of the file name e.g. if file IMG_20170404_170509.JPG is found twice the second instance will be rename IMG_20170404_170509_1.JPG and if another is found then it will be IMG_20170404_170509_2.JPG, etc.

Here is my current script:

exiftool -o . '-Directory<$createdate/${model;}' -d /volume1/Synology/Pictures/Pictures/post_2016/%Y/%m/ -r /volume1/photo/"input"/

Thanks

P.S. I know there is a < between -Directory and $Createdate in the above script but if I include it my message on this board is truncated (yes I know I'm a real novice at all this. Apologies).

StarGeek
  • 4,948
  • 2
  • 19
  • 30
spesmagna
  • 45
  • 7

1 Answers1

2

Because you want to change the filename as well as the directory, you'll have to use the Filename pseudo tag instead of the Directory pseudo tag.

Try this command:
exiftool -o . '-Filename<$createdate/${model;}/%f%+c%E' -d /volume1/Synology/Pictures/Pictures/post_2016/%Y/%m -r /volume1/photo/"$input"/

You can include directory paths when you are renaming a file (see 3rd paragraph of FileName and Directory tags). The %f holds the name of the file. The %c variable is a counter to be added in the case of a filename collision. Adding a plus sign to it will place an underscore in front of the counter number (see the docs on the -w option). The %E adds the extension, including the leading dot (a lowercase e would not include the leading dot).

StarGeek
  • 4,948
  • 2
  • 19
  • 30
  • Many thanks. I'll try this and revert back with feedback. – spesmagna Apr 09 '18 at 08:33
  • Additional info, you can use the `Testname` pseudo tag in place of `Filename` to test the results of a rename without actually processing the files. – StarGeek Apr 09 '18 at 14:54
  • Hi StarGeek. I tried your code and it works perfectly. Many thanks! – spesmagna Apr 13 '18 at 20:25
  • I have a follow up question. I need to sort the files twice: once as per the above script but a second time in a separate directory tree (for subsequent encryption and upload to cloud storage). It is the "final" file which I need to sort into the second directory tree NOT the original one (the two are most often the same except in the precise case where there was a duplicate file and the copied file was renamed - it is that renamed file I want to move to the new directory tree). Any suggestions on how I can do this? Thanks – spesmagna Apr 14 '18 at 13:12
  • Copy to Directory2 only if there was a filename collision in Directory1? No, I can't really think of a way with just exiftool. I would think this would need more of an actually script. I'm assuming that the files in Directory2 are deleted after encryption/upload? If so, I would think something like this: 1) Grab the current timestamp 2) run the above exiftool command, 3) get the names of any files that have the number copy pattern in them, the UnderscoreNumberDotJpg pattern, and are also newer than the timestamp saved at the start of the script. 4) Copy those files to Directory2. – StarGeek Apr 14 '18 at 15:32
  • Thanks StarGeek. I was thinking something similar: copy into directory 2 any file in directory 1 and its subdirectories which has a FileAccessDate < 30 minutes (as an example). That way any newly added file is copied to directory 2 for subsequent encryption and upload. But I can't find anywhere the exact command to use to identify files based on their FileAccessDate "age". Any idea? Thank you – spesmagna Apr 14 '18 at 16:07
  • Sorry for the delay in responding. I knew it was possible with exiftool, but couldn't remember how to do it. You might also look into a bash copy version as well, as it would probably be faster. [Here's an example](https://stackoverflow.com/a/37412139/3525475), but with move (`mv`) instead of copy (`cp`). In exiftool, you can try something like this: `exiftool -if '${FileAccessDate;DateFmt("%s")>${now;DateFmt("%s")-1800}" -o . '-Directory=/path/to/Directory2' ` Test it first. the 1800 number is the number of seconds in 30 minutes (30*60). Change as needed. – StarGeek Apr 15 '18 at 15:49