Vladimir Lemberg wrote:
Hi,

Hello,

Could you help me to find what's wrong in my script?

I need to compare two directories and if file exists in both - print it.

die "Usage: 2 arguments must be passed i.e. file1 file2\n" if @ARGV != 2;
opendir DIR1, $ARGV[0] or die "couldn't open $ARGV[0] for reading.\n";
opendir DIR2, $ARGV[1] or die "couldn't open $ARGV[1] for reading.\n";

foreach $filename(readdir DIR1) {
   next if $filename =~/^\./;
     while (readdir DIR2){
       if (-e $filename){
       print "$filename\n"}
     }
 }
closedir(DIR1);
closedir(DIR2);

Looks like I'm checking "if exists" in first directory but not in second.

No, you are checking "if exists" in neither directory.

die "Usage: 2 arguments must be passed i.e. dir1 dir2\n" if @ARGV != 2;
opendir DIR1, $ARGV[0] or die "couldn't open $ARGV[0] for reading.\n";

foreach my $filename ( readdir DIR1 ) {
    next if $filename =~ /^\./;
    if ( -e "$ARGV[1]/$filename" ) {
        print "$filename\n"}
    }
}
closedir DIR1;



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to