Compare File Name with other Files in the Directory

Started by Geoff_12889, July 27, 2023, 02:03:36 AM

Previous topic - Next topic

Geoff_12889

This should be my last hurdle before I set everything up how I like (for the time being).

I wonder if there is a way to check a specific file to see if it matches another file's name within the same directory (minus the file extension).

The idea is that if I already have a RAW photo, let's say: "2023-07-27 01.57.44 - SONY A73 - DSCF0001.ARW" in the folder, and I move a JPEG file: "2023-07-27 01.57.44 - SONY A73 - DSCF0001.JPG" into the folder, that it would be moved into another sub-folder "JPEG Backup". But if no other file matches, it remains in place.

I can do the folder organization and moving; I just don't know where to begin to check the file against all the other files in the directory to check if there is a match.

Not sure if there is any sort of functionality like this in ExifTool or if it would require some Perl scripting.

Thank you.

Phil Harvey

You could do this by first moving all the JPG files into the folders with the ARW files, then running this command:

exiftool -ext arw -directory="%dJPEG Backup" -srcfile %d%f.jpg -r DIR

You'll get errors for JPG files that are already in the correct location and missing JPG files, but you can ignore these.

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

Geoff_12889

I have other RAW formats like RAF, DNG, and GPR.

Could I run this instead to get the same result, instead of checking for ARW extension specifically?

--ext jpg

Phil Harvey

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

Geoff_12889

Thanks so much Phil.

I sent you a coffee through Paypal, not only for your fantastic tool but your charitable contributions.

Phil Harvey

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

Geoff_12889

#6
I'm still testing to try and fit this into my workflow. Right now, if I use the script you provided:

exiftool --ext jpg -directory="%dJPEG Backup" -srcfile %d%f.jpg -r DIR

but remove the -directory="%dJPEG Backup" any matched file runs the exiftool and in terminal displays all EXIF data; every other file it checks that doesn't match returns an error, as you mentioned it would.

My goal is I didn't necessarily want the file to get moved into JPEG Backup at this point but to run more test and functions on the file. I have little (zero) experience with Perl, so I am using AppleScript for all the parts that are outside the scope of exiftool.

Preferably I would have exiftool (or Perl) check if there is a filename that matches exactly with the JPEG and then return "TRUE" (or, as I've learned with Perl, a "0"?), then I can run the rest of my script based on if that passed or failed.

Phil Harvey

When you say "return true", if you mean the exit status returned after exiftool runs, then you would have to launch exiftool separately for each file you want to test.  Instead, it would be preferable to make a list of files, maybe like this:

exiftool --ext jpg -srcfile %d%f.jpg -p '$directory/$basename.jpg' -r DIR > out.txt

Then process the files listed in out.txt with your script.

- Phil

Edited to write to txt file instead of piping directly to a script because you mentioned AppleScript, and I don't know if you can pipe to AppleScript like this.
...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 ($).

Geoff_12889

I think your first approach is the more elegant way to do this; I'll just need to figure out regular expressions (or maybe File::Basename?) to pull the directory from the file getting checked.

The variable getting passed "$1" is the entire DIR/file.ext, so I'll need to separate file.ext from the variable so that DIR is just the path and not the file specifically.

Edit: I was hoping the tag "OriginalDirectory" could be used in place of the DIR at the end, but it is saying "Ignored superfluous tag name or invalid option: -OriginalDirectory"

Edit 2: To be more specific on what I am trying, here is the script I am using

exiftool ~/Desktop/1.jpg --ext jpg -directory="%dJPEG Backup" -srcfile %d%f.jpg -OriginalDirectory
(not sure if originaldirectory needs to be formated as in Perl '$OriginalDirectory', but I've tried it both ways and neither worked.

Geoff_12889

Not sure if it is related to File::Basename but the $filename is working but trying $dirs or $directories does not.

But $directory does  :P

Edit: I would have thought this would work, but it does not:

exiftool ~/Desktop/1.jpg --ext jpg -directory="%dJPEG Backup" -srcfile %d%f.jpg '$directory'

Edit 2: My thought process now is to use -userparam file="$1" ($1 being the DIR/file.ext parameter that is being passed for the file being processed) and then creating another -userparam path which would take the $file and run a regex on it to remove the file name, leaving me with just the path and then using that in place of DIR at the end of the script.
but honestly, I have no idea what I'm talking about here

Geoff_12889

I'm trying to use this now as an external Perl script. I was only using exiftool in the Terminal and it was working great, but now I have the regular expression being done in Perl, so I want to call ExifTool to use it within the script.

use Image::ExifTool;
my $exifTool = Image::ExifTool->new;

$path = "/Users/[username]/desktop/2023-07-08 __ 11.47.18 __ X100V __ DSCF0242.jpg";
$path =~ s{/\d{4}-\d{2}-\d{2} __ \d{2}.\d{2}.\d{2} __ \w+ __ \w+\.\w{3}$}{};

$exifTool --ext jpg -directory="%dJPEG Backup" -srcfile %d%f.jpg $path;

But I get the following error:

Can't locate Image/ExifTool.pm in @INC (you may need to install the Image::ExifTool module) (@INC contains: /Library/Perl/5.30/darwin-thread-multi-2level /Library/Perl/5.30 /Network/Library/Perl/5.30/darwin-thread-multi-2level /Network/Library/Perl/5.30 /Library/Perl/Updates/5.30.3 /System/Library/Perl/5.30/darwin-thread-multi-2level /System/Library/Perl/5.30 /System/Library/Perl/Extras/5.30/darwin-thread-multi-2level /System/Library/Perl/Extras/5.30) at test.pl line 1.
BEGIN failed--compilation aborted at test.pl line 1.

Phil Harvey

1. For "use Image::ExifTool" to work, its libraries need to be installed in one of the specified directories.  You can do this manually, or follow the Unix install instructions.

...but I don't think you want to do this.

2. You are trying to execute the "exiftool" Perl script from within your test Perl script, so you don't need to load the module.  Instead, execute "exiftool" as an external command:

#!/usr/bin/perl -w
my $path = "/Users/[username]/desktop/2023-07-08 __ 11.47.18 __ X100V __ DSCF0242.jpg";
$path =~ s{/\d{4}-\d{2}-\d{2} __ \d{2}.\d{2}.\d{2} __ \w+ __ \w+\.\w{3}$}{};

`/usr/local/bin/exifTool --ext jpg -directory="%dJPEG Backup" -srcfile %d%f.jpg "$path"`;

Normally from Perl one would create an Image::ExifTool object then call the member functions to do what you want, but then you would have to process the directory yourself because this is done inside the "exiftool" script.  For this reason, in this case it is simpler to just run the exiftool script.

Also, I have quoted "$path" because I think this is necessary if any of the directories contain a space.

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

Geoff_12889

In order for the rest of my script to run, I need it to return an exit code "0". Right now the script is behaving correctly, but I am getting an exit code "65280".

Is there a way to force it to return an exit code "0" after it's run the exiftool command?

Phil Harvey

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