I Will Do It By Hand One More Time

If you catch yourself saying this about a task for the third time, it is time to write a script for it. My contribution for today: when I want to process a large amount of photos my workflow usually starts with sorting through the contents of my SD cards and deleting the pictures with which I am not happy. If you are working on a rather dated computer as mine, having the corresponding RAW files in the same folder will cause you a headache with most picture viewers, especially when you try to flip through the files quickly. Moving them to a separate folder, however, will make you do the work twice. The following PurgeRaw script fixes this problem:

[code lang=”perl”]
#!/usr/bin/perl -w
use strict;

print “Searching for RAW files without matching JPG preview…\n”;

my @jpgs = glob(“*.jpg *.JPG”);
@jpgs or die “There are no preview JPGs in this directory.”;

my @raws = glob(“NEF/*.NEF RAW/*.NEF”);
@raws or die “There are no RAWs we could purge.”;

foreach (@jpgs) {
$_ =~ s/\..*//;
}

my @toDelete;
my $raw;
foreach (@raws) {
$raw = $_;
$_ =~ s/\..*//;
$_ =~ s/^.*\///;
if(!($_ ~~ @jpgs)){
push(@toDelete, $raw);
}
}

if(@toDelete){
print “Looks like we should delte the following RAW files:\n”;
foreach(@toDelete){print $_ . “\n”;}

print “Proceed with deleting? (Y/N): “;
my $answer = ;
chomp($answer);

if($answer eq “Y”){
print “Deleting…”;
foreach my $file ( @toDelete ) {
unlink $file or warn “Could not delete $file: $!”;
}
}
elsif($answer eq “N”){
print “Aborting without deleting anything.”;
}
else{
print “Wrong input – aborting without deleting anything.”;
}
}
else{
print “Did not find any RAW files eligible to be deleted.”
}
[/code]

This is obviously far from being anything sophisticated, but it does the job. If needed, you can grab the code from my github repository, that is also where I will add the next scripts when I catch myself counting to three again.

Leave a Reply

Your email address will not be published. Required fields are marked *