目錄我是新來使用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