2017-02-03 53 views
0

我从here代码比较中的所有文件出现在目录中递归的Perl DirCompare排除目录

#!/usr/bin/perl 

use strict; 
use warnings; 

use File::DirCompare; 
use File::Basename; 

sub compare_dirs 
{ 
    my ($dir1, $dir2) = @_; 
    my $equal = 1; 

    File::DirCompare->compare($dir1, $dir2, sub { 
    my ($a,$b) = @_; 
    $equal = 0; # if the callback was called even once, the dirs are not equal 

    if (!$b) 
    { 
     printf "File '%s' only exists in dir '%s'.\n", basename($a), dirname($a); 
    } 
    elsif (!$a) { 
     printf "File '%s' only exists in dir '%s'.\n", basename($b), dirname($b); 
    } 
    else 
    { 
     printf "File contents for $a and $b are different.\n"; 
    } 
    }); 

    return $equal; 
} 

print "Please specify two directory names\n" and exit if (@ARGV < 2); 
printf "%s\n", &compare_dirs($ARGV[0], $ARGV[1]) ? 'Test: PASSED' : 'Test: FAILED'; 

我要排除一些像名.old目录,的.pro等的差异

我在这里找不到办法做到这一点?

回答

0

溶液1

按照DirCompare documentation,你可以提供一个第四个参数比较功能(CMP),做一个自定义测试。

溶液2

的另一种方法是测试$ DIR1和用于排除目录名DIR2 $,以及设置$等于0仅当$ DIR1和DIR2 $不排除。

if (not_excluded($dir1) && not_excluded($dir2)) { 
$equal = 0; 
} 

(not_excluded被你写了一个自定义的功能,如果提供的目录不排除返回true)