Bug in Script - Comparing two directories recursively

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArchanaAshok
    New Member
    • Jul 2007
    • 3

    Bug in Script - Comparing two directories recursively

    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.
    Last edited by miller; Jul 18 '07, 09:33 PM. Reason: Code Tag and ReFormatting
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Well, my first suggestion is that any time you want to do anything in Perl, make sure to check CPAN before writing your own code. (That is, unless you are like me and you like re-inventing the wheel for the purpose of learning). That aside though, in this case I would definitely searh cpan for a module.

    If you do a search on cpan for "copy", you will find the File::Copy module. This is good, but doesn't help you with recursion. For that, you might want to read the module called File::Copy::Rec ursive
    .

    That module should do what you want to do as far as navigating a directory structure, you just have to code around it to do whatever else you need to do.

    Regards,

    Jeff

    Comment

    Working...