目录我是新来使用Perl所以原谅我noobness,比较两个使用Perl
这里就是我打算做的。
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;
}
我不能递归遍历子目录在上面代码的帮助下给定的目录。任何帮助,将不胜感激。
编辑:使用文件: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
我面对测试结果:失败。其结果是必须已经通过。任何人都可以请纠正我?
感谢
我基本上试图模仿UNIX的diff -r命令吗? – Kelly 2012-02-28 08:49:28