- 论坛徽章:
- 0
|
本帖最后由 kzled 于 2010-03-31 22:25 编辑
- use strict;
- use warnings;
- use Cwd;
- use File::Basename;
- use Digest::MD5;
- my $path = shift || getcwd();
- if ( ! -d $path ){
- $0 = basename($0);
- print <<HELP;
- USAGE:
- \tperl $0 path
- EXAMPLE:
- perl $0 d:\\
- perl $0 d:\\docs
-
- For more please contact me by liyuncn\@yahoo.com
- HELP
- exit;
- }
- # use to store fileNames and md5s, keys are md5s, values are filenames
- my %duplicates;
- &read($path);
- &output;
- sub read{
- my $dir = shift;
- my (@dirs, $size, $file);
-
- opendir (DIR, $dir) or die "Failed to open $dir: $!\n";
-
- LOOP:
- while ( $file = readdir(DIR) ){
-
- # Skipped system files start name with ...
- next if ($file =~ /^\./);
-
- if ( $dir =~ m/\\$/ ){
- $file = $dir.$file;
- }else{
- $file = $dir."\\".$file;
- }
-
- # Put dirs into an array, to recursive call read subroutine
- if ( -d $file ){
- push @dirs, $file;
- }else{
- # Got files size and put them in hash as key
- $size = -s $file;
-
- # Using $size as key, if $size exisit, => next steps
- if (exists $duplicates{$size}){
- my $md5;
- my @md5s;
-
- foreach ($file, $duplicates{$size}[0]){
-
- open( FILE, $_ ) or die "Failed to open $file: $!\n";
- binmode(FILE);
-
- # Get md5 value from files, make it keys of %duplicates
- $md5 = Digest::MD5->new->addfile(*FILE)->hexdigest;
-
- push @md5s, $md5;
- close FILE;
- }
-
- push ( @{$duplicates{$size}}, $file ) if ( $md5s[0] eq $md5s[1]);
-
- next;
- }
-
- $duplicates{$size} = [$file];
- }
- }
-
- # Close dir
- closedir DIR or die "Error happen at closing dir: $!\n";
-
- # Recursive call subroutine read
- foreach (@dirs){
- &read($_);
- }
- }
- sub output{
- my %dups = %duplicates;
-
- foreach my $key ( keys %dups ){
-
- # For each values array in $Dups hash, if more than 1 files, print them out
- if ($#{$dups{$key}}> 0){
- print "Duplicated files found:\n";
- print "-------------------------------------------------\n";
-
- # Print file details..
- foreach (@{$dups{$key}}){
- print $_."\n";
- }
- print "-------------------------------------------------\n";
- }
- }
- }
复制代码 |
|