CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
comparing files from 2 different folders

 



user1
User

Mar 30, 2005, 2:37 PM

Post #1 of 5 (561 views)
comparing files from 2 different folders Can't Post

I am able to compare one file from one folder to another file from another folder. It is working fine. I want to be able to compare all the files from one folder to all the files in another folder

Under Folder c:/Results

I have files in the following format

File1.txt

File2.txt

File3. txt

Under Folder c:/Results/Live

I have files in the following format

File1_live.txt

File2_live.txt

File3_live.txt

The comparisions should be done File1.txt Vs File1_live.txt

File2.txt Vs File2_live.txt

File3.txt Vs File3_live.txt

I can do only by giving file names to the below code. I want to be able to compare all the files in 2 folders.

#!/usr/perl/bin

my %words;


open FILE1, 'c:\Results\File1.txt' or die $!;


while (<FILE1>) { chomp; # Build a hash where the keys are the words


$words{$_}++ for split /:/; } close FILE1;



open FILE2, 'c:\Results\Live\File1_live.txt' or die $!;


while (<FILE2>) {


chomp; # Delete the words we've found from the hash

delete @words{split /:/}; } close FILE2;

print join '', keys %words; print "\n";

It is working fine.

Please help me how to compare all the files in both the folders

Thanks.


KevinR
Veteran


Mar 30, 2005, 4:05 PM

Post #2 of 5 (559 views)
Re: [user1] comparing files from 2 different folders [In reply to] Can't Post

untested:


Code
#!/usr/perl/bin 

opendir(DIR, "c:\\Results") or die "Can't open c:\\Results: $!";
my @files = grep (/^File\d+\.txt$/, readdir DIR);
close (DIR);

foreach (@files) {
/^(File\d+)\.txt$/;
my $file2 = $1;
my %words = ();
open(FILE, "c:\\Results\\$_") or die "$!";
while (<FILE>) {
chomp; # Build a hash where the keys are the words
$words{$_}++ for split(/:/);
}
close(FILE);
open(FILE2, "c:\\Results\\Live\\$file2" . "_live.txt") or die "$!";
while (<FILE2>) {
chomp; # Delete the words we've found from the hash
delete @words{split(/:/)};
}
close(FILE2);
print join('', keys %words);
print "\n";
}

-------------------------------------------------


user1
User

Mar 31, 2005, 8:09 AM

Post #3 of 5 (548 views)
Re: [KevinR] comparing files from 2 different folders [In reply to] Can't Post

I am not able to get $file2 from the below code and the line

open(FILE, "c:\\Results\\$_") or die "$!"; gets permission denied error.

foreach (@files) {
/^(File\d+)\.txt$/;
my $file2 = $1;
my %words = ();
open(FILE, "c:\\Results\\$_") or die "$!";



KevinR
Veteran


Mar 31, 2005, 10:31 AM

Post #4 of 5 (544 views)
Re: [user1] comparing files from 2 different folders [In reply to] Can't Post

print $_ to see what its value is.
-------------------------------------------------


(This post was edited by KevinR on Mar 31, 2005, 10:35 AM)


user1
User

Mar 31, 2005, 12:10 PM

Post #5 of 5 (539 views)
Re: [KevinR] comparing files from 2 different folders [In reply to] Can't Post

Thank you. I was able to get the value from $file2. The above worked fine.



Thanks,

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives