Hi All,
I have done a script to compare 2 similar directories containing equal number of files, and to compare the content of those files in the directories.
But there is some problem in the recursion. Can anybody findout where I am going wrong?
Here is the code.
[CODE=perl]
#!/usr/bin/perl
sub dirparse{
our $scalar;
our @content1, @content2;
opendir DH1, $path1;
for $list1 (sort readdir(DH1)) {
print $list1,"\n";
if ($list1 ne "." && $list1 ne "..") {
if (-d $path1.$list1) {
print "Inside directory recursion\n";
print "List1 is :",$list1,"\ n";
$path1 = $path1.$list1."/";
dirparse;
}
$pat1 = $path1.$list1;
$path1{$list1} = $pat1;
print "pat1 : ", $pat1,"\n";
@array1 = (@array1 , $list1);
}
}
opendir DH2, $path2;
for $list2 (sort readdir(DH2)) {
if ($list2 ne "." && $list2 ne "..") {
if (-d $path2.$list2 ) {
$path2 = $path2.$list2."/";
dirparse;
}
$pat2 = $path2.$list2;
$path2{$list2} = $pat2;
@array2 = (@array2, $list2);
}
}
$number1 = scalar @array1;
$number2 = scalar @array2;
print "Array 1 is ",@array1," \n";
if ($number1 == $number2){
print "Equal number of files \n";
}
$number1 = $number1-1;
for (0..$number1) {
if($array1[$_] ne $array2[$_]) {
die "Two files are not same name \n";
}
#print $array1[$_],"\n";
#print %path1,"\n";
#print %path2, "\n";
if(-d $path1{$array1[$_]}) {
next;
}
print $path1{$array1[$_]};
open FILE1,$path1{$a rray1[$_]} or die "Can't open file: $_";
open FILE2,$path2{$a rray2[$_]} or die "Can't open file: $_";
@content1 = <FILE1>;
@content2 = <FILE2>;
print "For the file ", $array1[$_], " :";
print $_+1," : Files are compared:result :\n";
$con = scalar @content1;
for $scalar (0..$con) {
if (($content1[$scalar] cmp $content[$scalar]) != 0) {
print " Not Equal \n";
last;
}
}
if ($scalar == $con) {
print "Equal"
}
}
closedir(DH1);
closedir(DH2);
}
our $path1;
our $path2;
$path1 = "E:/perl/archana1/";
$path2 = "E:/perl/archana2/";
our %path1;
our %path2;
print "Hi : your directory matching is on process ...\n";
dirparse;
[/CODE]
All suggestions are welcome as I am new to perl programming.
I have done a script to compare 2 similar directories containing equal number of files, and to compare the content of those files in the directories.
But there is some problem in the recursion. Can anybody findout where I am going wrong?
Here is the code.
[CODE=perl]
#!/usr/bin/perl
sub dirparse{
our $scalar;
our @content1, @content2;
opendir DH1, $path1;
for $list1 (sort readdir(DH1)) {
print $list1,"\n";
if ($list1 ne "." && $list1 ne "..") {
if (-d $path1.$list1) {
print "Inside directory recursion\n";
print "List1 is :",$list1,"\ n";
$path1 = $path1.$list1."/";
dirparse;
}
$pat1 = $path1.$list1;
$path1{$list1} = $pat1;
print "pat1 : ", $pat1,"\n";
@array1 = (@array1 , $list1);
}
}
opendir DH2, $path2;
for $list2 (sort readdir(DH2)) {
if ($list2 ne "." && $list2 ne "..") {
if (-d $path2.$list2 ) {
$path2 = $path2.$list2."/";
dirparse;
}
$pat2 = $path2.$list2;
$path2{$list2} = $pat2;
@array2 = (@array2, $list2);
}
}
$number1 = scalar @array1;
$number2 = scalar @array2;
print "Array 1 is ",@array1," \n";
if ($number1 == $number2){
print "Equal number of files \n";
}
$number1 = $number1-1;
for (0..$number1) {
if($array1[$_] ne $array2[$_]) {
die "Two files are not same name \n";
}
#print $array1[$_],"\n";
#print %path1,"\n";
#print %path2, "\n";
if(-d $path1{$array1[$_]}) {
next;
}
print $path1{$array1[$_]};
open FILE1,$path1{$a rray1[$_]} or die "Can't open file: $_";
open FILE2,$path2{$a rray2[$_]} or die "Can't open file: $_";
@content1 = <FILE1>;
@content2 = <FILE2>;
print "For the file ", $array1[$_], " :";
print $_+1," : Files are compared:result :\n";
$con = scalar @content1;
for $scalar (0..$con) {
if (($content1[$scalar] cmp $content[$scalar]) != 0) {
print " Not Equal \n";
last;
}
}
if ($scalar == $con) {
print "Equal"
}
}
closedir(DH1);
closedir(DH2);
}
our $path1;
our $path2;
$path1 = "E:/perl/archana1/";
$path2 = "E:/perl/archana2/";
our %path1;
our %path2;
print "Hi : your directory matching is on process ...\n";
dirparse;
[/CODE]
All suggestions are welcome as I am new to perl programming.
Comment