Am Dienstag, 10. Mai 2005 08.34 schrieb [EMAIL PROTECTED]:
> Hi All,
>
> I'm trying to find a way to compare the files and folders contained within
> 2 separate directory structures to determine whether they are identical or
> not. For this I tried using the File::Find module but got stuck at a
> certain point. I looked online and in some books but couldnt find a way so
> I'm hoping you can help me.
>
> The code looks like as follows.
>
> use File::Find;
> .........
> .........
> my $dir1 = 'c:\perl';
> my $dir2 = 'c:\temp\perl';
>
> find (\&wanted, $dir1, $dir2);
> sub wanted()
> {
>  my @array;
>  push @array, $_;     # <------- problem here. How can I create 2 arrays
> for $dir1 and $dir2.

See the man page for File::Find, "The wanted function":

       Note that despite its name, the
       "wanted()" function is a generic callback function, and does not tell    
    
       File::Find if a file is "wanted" or not. 
       [...]
       $File::Find::dir is the current directory name,
       $_ is the current filename within that directory
       $File::Find::name is the complete pathname to the file.

You could, within wanted(), use $File::Find::dir to decide wheter $_ lies in 
the first or in the second directory tree and fill two arrays.

But I'm in doubt if a wanted() callback is necessary: you could just build two 
hashes by calling find() separatly for both directory trees and then compare 
them to find the 3 possibilities:
- files present in both locations
- file only present in the first dir tree
- file only present in the 2nd dir tree

Something like (untested):

# build hashes with pairs (filename=>0)
#
my %first=map {$_=>1} find (sub {}, $dir1);
my %second=map {$_=>1} find (sub {}, $dir2);

# build another hash containing differences
#
my %diff;
$diff{$_}-- for keys %first;
$diff{$_}++ for keys %second;

Now, %diff should contain all file names found in either dir tree, with 
values:
0: contained in both
-1: contained only in $dir1
+1: contained only in $dir2

joe

>
> .....
> .....
> }
>
> As you can see above, I've passed both directories as arguments but what
> this does is that it creates a single array with both $dir1 and $dir2
> contents. What I'm looking for is a way to move the contents of $dir1 into
> @array1 and contents of $dir2 into @array2. Thank you very much in advance.
> Ahmer

-- 
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