ATaylor has asked for the wisdom of the Perl Monks concerning the following question:
Thank you for any help, Aaron Taylor#!/usr/bin/perl -w # # Reads contents of a directory # in array context. # # Usage: # readdir4.pl source_directory, destination_directory # use File::Copy; # remove the comment below if you want to read in the directory from a +nd arg $name1 = $ARGV[0]; $name2 = $ARGV[1]; opendir(SOURCE, $name1) or die "Can't open $name1 due to $!"; @source_entries = readdir(SOURCE); closedir(SOURCE); # Lets get the destination now opendir(DESTINATION, $name2) or die "Can't open $name2 due to $!"; @dest_entries = readdir(DESTINATION); closedir (DESTINATION); # Sort Results. @sourcesorted = sort(@source_entries); @destsorted = sort (@dest_entries); # Make a hash out of them foreach $filename1 (@sourcesorted) { %hashsource = ($filename1,$filename1); } foreach $filename2 (@destsorted) { %hashdest = ($filename2,$filename2); } # At this point we have two sorted arrays that we can use to do compar +es with # The idea now is to compare Source with Destination first. # If the file exist in Source and not in Destination then copy it over + to Destination # If the file exist in both then do nothing. # Then compare Destination with Source. If the file exist in Destinati +on and not in source # then erase it from Destination. # this routine will seperate all the files into two arrays @union = @intersection = @difference = (); %count = (); foreach $element (@sourcesorted, @destsorted) { $count{$element}++ + } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference } +, $element; } # Sorting the difference array @diffsorted = sort(@difference); # Now lets compare the files. We can ignore all the files in the inter +section list # and compare only the files in the difference list. # The trick is to use the @difference array correctly foreach $filename (@diffsorted) { # now that we are going through each element in the @differenc +e array lets # do some checking. # Now for every file in the sorted source list see if it is no +t equal to a file # file in the differences list. If it is in the differences li +st and it is not in the # source list then it should be deleted from the destination d +irectory. unless ( exists $hashdest{$filename}){ if ( exists $hashsource{$filename}){ print "$filename exist in the source directory.\n"; print "Not in the destination directory, so copy it.\n\n"; #now to do actual work in here. chdir($name1); print "Now copying, $filename.\n"; #copy($filename,$name2.$filename) or die # "Can't copy $filename to $name2$filename due to $! +.\n"; } } } foreach $filename (@diffsorted) { # continuation of above test. This one checks for stragglers i +n the # destination directory if ( exists $hashdest{$filename}){ unless ( exists $hashsource{$filename}) { print "$filename exist in destination directory.\n"; print "Not in the source directory, so delete it. \n +\n"; # Lets change to the right directory # chdir($name2); # and delete unlink($name.$filename); print "Deleted $filename.\n"; } } } foreach $entry (@sourcesorted) { print "Source: \t$entry\n"; } foreach $entrydest (@destsorted) { print "Destination: \t$entrydest\n"; } foreach $entryinter (@intersection) { print "Intersection: \t$entryinter\n"; } foreach $entrydiff (@diffsorted) { print "Differences: \t$entrydiff\n"; } # readdir4.pl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New Perl programmer looking for answer in Directory Comparisons
by merlyn (Sage) on May 01, 2001 at 19:49 UTC | |
|
Re: New Perl programmer looking for answer in Directory Comparisons
by chromatic (Archbishop) on May 01, 2001 at 19:51 UTC | |
by ATaylor (Initiate) on May 01, 2001 at 19:56 UTC | |
|
Re: New Perl programmer looking for answer in Directory Comparisons
by lindex (Friar) on May 01, 2001 at 21:05 UTC | |
by merlyn (Sage) on May 01, 2001 at 21:10 UTC | |
by lindex (Friar) on May 01, 2001 at 21:26 UTC | |
|
Re: New Perl programmer looking for answer in Directory Comparisons
by ZZamboni (Curate) on May 01, 2001 at 22:39 UTC |