home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / netprofessional / code / 02.02 / MacPerl.FindDupes.perl.txt < prev    next >
Text File  |  2010-09-21  |  787b  |  29 lines

  1. # FindDuplicates - find (possible) duplicate files
  2. #
  3. # prints:  MyDisk:FileTree:foo (123,456)
  4. #          MyDisk:FileTree:bar (123,456)
  5. #          ...
  6.  
  7. use File::Find;
  8.  
  9. find(\&makeFileKey, "MyDisk:FileTree");  # collect info
  10.  
  11. foreach $key (sort(keys(%out))) {   # print results
  12.   printf("%s\n", $out{$key}) if ($cnt{$key} > 1);
  13. }
  14.  
  15. sub makeFileKey {                 # examine $_
  16.   if (-f($_)) {                     # is it a file?
  17.     open(FILE, $_);                 # checksum it
  18.     read(FILE, $in, 1000);          # first KB only
  19.     $sum = unpack("%32C*", $in) % 32767; # make the checksum
  20.     close(FILE);
  21.   
  22.     $siz = -s($_);                  # get file size
  23.     $key = "$siz,$sum";             # build a key
  24.     $cnt{$key}++;
  25.     $out{$key} .= "$File::Find::name ($key)\n";
  26.   }
  27. }
  28.  
  29.