【发布时间】:2018-04-03 13:15:29
【问题描述】:
我是 Perl 新手,请原谅我的菜鸟,
这就是我打算做的。
$ perl dirComp.pl dir1 dir2
dir1 & dir2 是目录名。
脚本 dirComp.pl 应该识别 dir1 和 dir2 中的内容是否相同。
我想出了一个算法
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;
}
在上述代码的帮助下,我无法递归遍历给定目录中的子目录。任何帮助将不胜感激。
编辑:使用 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
我正面临测试结果:失败。结果肯定是通过了。有人可以纠正我吗?
谢谢
【问题讨论】:
标签: perl