使用 Perl 比较两个目录 [英] Comparing two directories using Perl

查看:61
本文介绍了使用 Perl 比较两个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Perl 的新手,请原谅我的菜鸟,

i am new to Perl so excuse my noobness,

这是我打算做的.

$ perl dirComp.pl dir1 dir2

dir1 &dir2 是目录名.

dir1 & dir2 are directory names.

脚本 dirComp.pl 应该识别 dir1 & 中的内容是否存在dir2 是否相同.

The script dirComp.pl should identify whether contents in dir1 & dir2 are identical or not.

我想出了一个算法

Store all the contents of dir1(recursively) in a list
Store all the contents of dir2 in another list
Compare the two list, if they are same - dir1 & dir2 are same else not.

my @files1 = readdir(DIR1h);
my @files2 = readdir(DIR2h);

    # Remove filename extensions for each list.

        foreach my $item (@files1) {
        my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/\.[^.]*/);
        $item = $fileName;
        }


        foreach my $item (@files2) {
        my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/\.[^.]*/);
        $item = $fileName;
        }

在上述代码的帮助下,我无法递归遍历给定目录中的子目录.任何帮助将不胜感激.

I am not able to recursively traverse subdirectories in a given directory with the help of above code. Any help would be appreciated.

使用 File:DirCompare

Using File:DirCompare

#!/usr/bin/perl -w

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

if ($#ARGV < 1 )
{
        &usage;
}

my $dir1 = $ARGV[0];
my $dir2 = $ARGV[1];

File::DirCompare->compare($dir1,$dir2,sub {
        my ($a,$b) = @_;
        if ( !$b )
        {
                printf "Test result:PASSED.\n";
                printf "Only in %s : %s\n", dirname($a), basename($a);
        }elsif ( !$a ) {
                printf "Test result:PASSED.\n";
                printf "Only in %s : %s\n", dirname($b), basename($b);
        }else {
                printf "Test result:FAILED.\n";
                printf "Files $a and $b are different.\n";
        }
});

我的目录结构如下,

dir1/                  dir2/
    --file1.txt            --file1.txt
    --file2.txt            --file2.txt
    --file3.cpp            --file3.cpp

我面临测试结果:失败.结果肯定是通过了.任何人都可以纠正我吗?

I am facing Test result:FAILED. As the result must have been passed. Can anyone please correct me?

谢谢

推荐答案

您提供的示例使用 File::DirCompare 按预期工作.

The example you supplied using File::DirCompare works as intended.

请记住,回调子例程会为每个目录中的每个唯一文件以及内容不同的每对文件调用.有相同的文件名是不够的,每个目录中每个文件的内容也必须完全相同.

Keep in mind that the callback subroutine is called for every unique file in each directory and for every pair of files which differ in their content. Having the same filename is not enough, the contents of each file in each directory must be exactly the same as well.

此外,您报告PASSED"的情况根本不成功(根据您的定义),因为它们详细说明了文件存在于其中一个目录中但不存在于另一个目录中的情况:即目录' 内容不相同.

Furthermore, the cases in which you report "PASSED" aren't a success at all (by your definition) since they detail the cases in which a file exists in one of the directories, but not the other: meaning the directories' contents are not identical.

这应该更接近你想要的:

This should be closer to what you want:

#!/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';

这篇关于使用 Perl 比较两个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆