Copyright 2022 - Custom text here

del_dup_photos.pl

#!/tools/perl/bin/perl

# Virer le .txt de la liste des fichiers à scanner

# attrib -R /S
use File::Basename;
use Digest::MD4;
use Digest::HMAC_MD5 qw(hmac_md5 hmac_md5_hex);
use Image::Magick;

#$base = $ARGV[0];

$base = "F:\\photos\\photos classées\\2003";

$base =~ s/\\/\//g;
$i=0;
&change($base);

print "\n\n$i files performed\n";


sub change($) {
        my $file = shift;

        if (-d $file && $file !~ /\/selected/ ) {
                # Local variables.
                local *D;
                my    $f;
                # Open the directory, and check its result value.
                opendir D, $file || die "Cannot open $file:";
                #print "$file\n";
                # Loop over the files in the directory.
                foreach $f (readdir D) {
                        # We leave the special directories alone, or we'll never finish.
                        next if ($f =~ /^\.{1,2}$/ );
                        # Go in recursion.
#                        print "f=$f\n";
                        &change("$file/$f");
                }
                # Close dir and exit call.
                closedir D || die "Cannot close $file: $!";
        }
        else
        {
                if ( $file =~ /.jpg/i )
                {
                        &perform($file);
                }
        }
}

sub md5($)
{
    my $file = shift;
    open(FILE, $file) or die "Can't open '$file': $!";
    binmode(FILE);
        $key = "abcd";
        
        $hmac = Digest::HMAC_MD5->new($key);
    $hmac->addfile(*FILE);

    close(FILE);
    return $hmac->hexdigest;
}

sub md4($)
{
    my $file = shift;
    open(FILE, $file) or die "Can't open '$file': $!";
    binmode(FILE);
    $md4 = Digest::MD4->new;
    $md4->add(FILE);
    close(FILE);
    return $md4->hexdigest;
}


sub perform($)
{
    local ($filename) = shift;
    local($dir);
    local($name);
    
    local($filename_1) = $filename;
    $filename_1 =~ s/\.jpg/_2.jpg/i;
    if( -f $filename_1 ){
    
            ($name,$dir,$ext) = fileparse($filename, '\..*');
            
            ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
            $sz_md4 = md5($filename) . "-" . $size;

            ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size_1,$atime,$mtime,$ctime,$blksize,$blocks) = stat($filename_1);
            $sz_md4_1 = md5($filename_1) . "-" . $size_1;
            if( $sz_md4 eq $sz_md4_1 ){
                $i++;
                print "$filename $filename_1 - $sz_md4 $sz_md4_1\n";
                unlink $filename_1;
            }
    }
}

sub mkdirp
{
    my($directory, $mode) = @_;
    my @dirs = split(/\//, $directory);
    my $path = shift(@dirs);   # build it as we go
    my $result = 1;   # assume it will work

    print "dir=$directory\n";
    
    unless (-d $path) {
         $result &&= mkdir($path, $mode);
    }

    foreach (@dirs) {
         $path .= "/$_";
         if ( ! -d $path) {
             $result &&= mkdir($path, $mode);
         }
    }

    return $result;
}
f t g m