Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Comparing two directories
Hi,

 

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.

 

-Vladimir

Report this thread to moderator Post Follow-up to this message
Old Post
Vladimir Lemberg
10-28-04 08:55 AM


Re: Comparing two directories
Actually, this is my script:

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


----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: <beginners@perl.org>
Sent: Wednesday, October 27, 2004 5:14 PM
Subject: Comparing two directories


Hi,



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.



-Vladimir

Report this thread to moderator Post Follow-up to this message
Old Post
Vladimir Lemberg
10-28-04 08:55 AM


Re: Comparing two directories
----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: "Vladimir Lemberg" <lemba@sbcglobal.net>; <beginners@perl.org>
Sent: Wednesday, October 27, 2004 8:16 PM
Subject: Re: Comparing two directories

Hello,
I beleave the two codes you have posted will yield identical results, as
there are a few problems . let me go through it and comment


> Actually, this is my script:
>
> foreach $filename(readdir DIR1) {
remember this will read entire DIR1, and triverse through the list of
files in created list

>  next if $filename =~/^\./;
>   while (readdir DIR2){
>   print "$filename\n" if (-e $filename);
>   last;
this will take effect first time through, which means two things
1. You will only check 1 filename from second directory each time
through
2. You will  check next file in DIR2 directory next time through
if you want to go through this route, you will need to rewind directory
handle each time you brake from this loop [ perldoc -f rewinddir ]
>   }
> }

You should probably read the entire directory contents at a time....here is
example on how one can achieve your goal.

#!PERl

use warnings;
use strict;
use Data::Dumper;
$|=1;

my %hash;

opendir RD,"." or die "ERROR: $!\n";
opendir RD2,".." or die "ERROR: $!\n";

my @DIR1 = readdir RD;
my @DIR2 = readdir RD2;


foreach my $filename (@DIR1) {

next if $filename =~/^\./;

foreach (@DIR2){

print "$filename\n" if $filename eq $_ ;

}

}



>
>
> ----- Original Message -----
> From: "Vladimir Lemberg" <lemba@sbcglobal.net>
> To: <beginners@perl.org>
> Sent: Wednesday, October 27, 2004 5:14 PM
> Subject: Comparing two directories
>
>
> Hi,
>
>
>
> 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.
>
>
>
> -Vladimir
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Mark Goland
10-28-04 08:55 AM


RE: Comparing two directories
I think the code can be further optimized without traversing through 2nd
directory as below.

opendir RD,"." or die "ERROR: $!\n";
my $dir2 = "../";

my @DIR1 = readdir RD;
foreach my $filename (@DIR1) {

next if $filename =~/^\./;

my $fname = $dir2 . $filename;
if (-e $fname) {
print "$filename\n" if $filename eq $_ ;
}
}


-----Original Message-----
From: Mark Goland [mailto:mgoland@optonline.net]
Sent: Thursday, October 28, 2004 9:29 AM
To: Vladimir Lemberg; beginners@perl.org
Subject: Re: Comparing two directories



----- Original Message -----
From: "Vladimir Lemberg" <lemba@sbcglobal.net>
To: "Vladimir Lemberg" <lemba@sbcglobal.net>; <beginners@perl.org>
Sent: Wednesday, October 27, 2004 8:16 PM
Subject: Re: Comparing two directories

Hello,
I beleave the two codes you have posted will yield identical results, as
there are a few problems . let me go through it and comment


> Actually, this is my script:
>
> foreach $filename(readdir DIR1) {
remember this will read entire DIR1, and triverse through the list of
files in created list

>  next if $filename =~/^\./;
>   while (readdir DIR2){
>   print "$filename\n" if (-e $filename);
>   last;
this will take effect first time through, which means two things
1. You will only check 1 filename from second directory each time
through
2. You will  check next file in DIR2 directory next time through
if you want to go through this route, you will need to rewind directory
handle each time you brake from this loop [ perldoc -f rewinddir ]
>   }
> }

You should probably read the entire directory contents at a time....here is
example on how one can achieve your goal.

#!PERl

use warnings;
use strict;
use Data::Dumper;
$|=1;

my %hash;

opendir RD,"." or die "ERROR: $!\n";
opendir RD2,".." or die "ERROR: $!\n";

my @DIR1 = readdir RD;
my @DIR2 = readdir RD2;


foreach my $filename (@DIR1) {

next if $filename =~/^\./;

foreach (@DIR2){

print "$filename\n" if $filename eq $_ ;

}

}



>
>
> ----- Original Message -----
> From: "Vladimir Lemberg" <lemba@sbcglobal.net>
> To: <beginners@perl.org>
> Sent: Wednesday, October 27, 2004 5:14 PM
> Subject: Comparing two directories
>
>
> Hi,
>
>
>
> 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.
>
>
>
> -Vladimir
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>



Report this thread to moderator Post Follow-up to this message
Old Post
Mallik
10-28-04 08:55 AM


Re: Comparing two directories
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
10-28-04 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:23 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.