亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 1918 | 回復(fù): 6
打印 上一主題 下一主題

閑時(shí)無(wú)聊,自己寫的查找相同文件的腳本 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2010-03-27 10:03 |只看該作者 |倒序?yàn)g覽
本帖最后由 kzled 于 2010-03-31 22:25 編輯
  1. use strict;
  2. use warnings;
  3. use Cwd;
  4. use File::Basename;
  5. use Digest::MD5;

  6. my $path = shift || getcwd();

  7. if ( ! -d $path ){
  8.         $0 = basename($0);
  9.         print <<HELP;
  10. USAGE:
  11. \tperl $0 path

  12. EXAMPLE:
  13.         perl $0 d:\\
  14.         perl $0 d:\\docs
  15.        
  16. For more please contact me by liyuncn\@yahoo.com
  17. HELP

  18.         exit;
  19. }

  20. # use to store fileNames and md5s, keys are md5s, values are filenames
  21. my %duplicates;

  22. &read($path);
  23. &output;


  24. sub read{
  25.         my $dir = shift;
  26.         my (@dirs, $size, $file);
  27.        
  28.         opendir (DIR, $dir) or die "Failed to open $dir: $!\n";
  29.        
  30.         LOOP:
  31.         while ( $file = readdir(DIR) ){
  32.                
  33.                 # Skipped system files start name with ...
  34.                 next if ($file =~ /^\./);
  35.                
  36.                 if ( $dir =~ m/\\$/ ){
  37.                         $file = $dir.$file;
  38.                 }else{
  39.                         $file = $dir."\\".$file;
  40.                 }
  41.                
  42.                 # Put dirs into an array, to recursive call read subroutine
  43.                 if ( -d $file ){
  44.                         push @dirs, $file;
  45.                 }else{
  46.                         # Got files size and put them in hash as key
  47.                         $size = -s $file;
  48.                        
  49.                         # Using $size as key, if $size exisit, => next steps
  50.                         if (exists $duplicates{$size}){
  51.                                 my $md5;
  52.                                 my @md5s;
  53.                                
  54.                                 foreach ($file, $duplicates{$size}[0]){
  55.                                        
  56.                                         open( FILE, $_ )  or die "Failed to open $file: $!\n";
  57.                                         binmode(FILE);
  58.                        
  59.                                         # Get md5 value from files, make it keys of %duplicates
  60.                                         $md5 = Digest::MD5->new->addfile(*FILE)->hexdigest;
  61.                                        
  62.                                         push @md5s, $md5;
  63.                                         close FILE;
  64.                                 }
  65.                                
  66.                                 push ( @{$duplicates{$size}}, $file ) if ( $md5s[0] eq $md5s[1]);
  67.                                
  68.                                 next;
  69.                         }
  70.                        
  71.                         $duplicates{$size} = [$file];
  72.                 }
  73.         }
  74.        
  75.         # Close dir
  76.         closedir DIR or die "Error happen at closing dir: $!\n";
  77.        
  78.         # Recursive call subroutine read
  79.         foreach (@dirs){
  80.                 &read($_);
  81.         }
  82. }

  83. sub output{
  84.         my %dups = %duplicates;
  85.        
  86.         foreach my $key ( keys %dups ){
  87.                
  88.                 # For each values array in $Dups hash, if more than 1 files, print them out
  89.                 if ($#{$dups{$key}}> 0){
  90.                         print "Duplicated files found:\n";
  91.                         print "-------------------------------------------------\n";
  92.                        
  93.                         # Print file details..
  94.                         foreach (@{$dups{$key}}){
  95.                                 print $_."\n";
  96.                         }
  97.                         print "-------------------------------------------------\n";
  98.                 }
  99.         }
  100. }


復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2010-03-27 10:23 |只看該作者
不錯(cuò)的

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2010-03-27 10:31 |只看該作者
{:3_190:}我是想拋磚引玉的。。。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2010-03-27 19:01 |只看該作者
好似好長(zhǎng)··

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2010-03-27 21:22 |只看該作者
本帖最后由 kzled 于 2010-03-31 22:26 編輯

長(zhǎng)?才一百行?
----

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2010-03-28 09:11 |只看該作者
回復(fù) 5# kzled
  1. if ( $file =~ /\.doc$/ || $file =~ /\.xls$/ || $file =~ /\.txt$/ || $file =~ /\.ppt$/ || $file =~ /\.conf/){
  2.                                  $type = "docs";
  3.                          }
復(fù)制代碼
這樣寫不累么   呵呵
換成  $file =~ /\.(doc|xls|txt)$/    這樣
或者  放到hash里  然后 print $hash->{$exe} if(exists $hash->{$exe})
應(yīng)該可以少寫一些

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2010-03-28 11:43 |只看該作者
回復(fù) 6# guap514


     嗯,謝謝,提醒,這樣可以省了不少代碼,寫這個(gè)腳本的時(shí)候,壓根沒怎么看正則表達(dá)式,所以現(xiàn)在代碼看起來(lái)相當(dāng)難看。。。呵呵
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP