If you don't provide $ARGV[1] (i.e 'remove') on the command line then the code would not enforce removal of any duplicate files...I've changed a little bit in the design of the code to make it easier for me when reading from a fileMD5.txt instead of having to provide for it through $ARGV[0]..
I could not see the reason why a data structure like "$seen{"$md5:$filesize"}" should be used so instead I made the hash key be the filename only, considering that a duplicate file is identified by its name...
The test text file 'fileMD5.txt' contains the following data:
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
e01d4d804d454dd1fb 296663 pics/20041004kidscamera/im001021.jpg im
+001021.jpg
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
e01d4d804d454dd1fb 296663 pics/20041004kidscamera/im001021.jpg im
+001021.jpg
e01d4d804d454dd1fb 296663 pics/20041004kidscamera/im001021.jpg im
+001021.jpg
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
8e773d2546655b84dd 304048 pics/20041004kidscamera/im001020.jpg im
+001020.jpg
e01d4d804d454dd1fb 296663 pics/20041004kidscamera/im001021.jpg im
+001021.jpg
e01d4d804d454dd1fb 296663 pics/20041004kidscamera/im001021.jpg im
+001021.jpg
Here is just an illustration of a concept that you can incorporate, it gives you a list of unique files on the STDOUT console and a list of those duplicates would be printed onto a temp file, my approach is open for criticism, modification and comments for I am a learner myself, best of luck:
use strict;
use warnings;
my $fileCNT = 0;
my $dupCNT = 0;
my $origCNT = 0;
my %seen;
my %duplicates;
open IN,"< fileMD5.txt" or die ".. we don't see a file to read\n";
print " ... starting to read find duplicats in fileMD5.txt.....\n\n\n"
+;
while(<IN>){
my $line = $_;
chomp $line;
$fileCNT++;
my ($md5,$filesize,$pathfile,$file) = split /\s+/,$line;
if(exists $seen{"$file"}){
$dupCNT++;
push @{$duplicates{"$file"}}, ([$pathfile,$filesize]);
}else{
push @{$seen{"$file"}}, ([$md5,$pathfile,$filesize]);
$origCNT++;
}
}
open (TEMP, '>>', "_Duplicate.temp") or die("can not create temporary
+file\n");
print TEMP "FilePath",' ' x 40,"File Size\n\n";
for my $key(keys %duplicates){
my $lineNo=1;
for my $info(@{$duplicates{$key}}){
print TEMP "@{[$lineNo++]}:@$info\n\n\n";
}
}
print "\U\t\toriginal files:\E\n\n";
print "FileName",' 'x20,"Location",' 'x 20,"size\n";
print "_" x 70,"\n";
for my $key(keys %seen){
my $lineNo = 1;
for my $info(@{$seen{$key}}){
print "@$info\n";
}
}
print "\n\n\USummary\E:files: $fileCNT originals: $origCNT files dup
+licated: $dupCNT.\E\n\n";
#use Data::Dumper;
#print Dumper(\%duplicates);
#print Dumper(\%seen);
Output to console:
... starting to read find duplicats in fileMD5.txt.....
ORIGINAL FILES:
FileName Location size
____________________________________________________________________
8e773d2546655b84dd pics/20041004kidscamera/im001020.jpg 304048
e01d4d804d454dd1fb pics/20041004kidscamera/im001021.jpg 296663
SUMMARY:files: 12 originals: 2 files duplicated: 10.
Output to temp file:
FilePath File Size
1:pics/20041004kidscamera/im001020.jpg 304048
2:pics/20041004kidscamera/im001020.jpg 304048
3:pics/20041004kidscamera/im001020.jpg 304048
4:pics/20041004kidscamera/im001020.jpg 304048
5:pics/20041004kidscamera/im001020.jpg 304048
6:pics/20041004kidscamera/im001020.jpg 304048
1:pics/20041004kidscamera/im001021.jpg 296663
2:pics/20041004kidscamera/im001021.jpg 296663
3:pics/20041004kidscamera/im001021.jpg 296663
4:pics/20041004kidscamera/im001021.jpg 296663
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
|