#!/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 and 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 compares 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 Destination 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 intersection 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 @difference array lets # do some checking. # Now for every file in the sorted source list see if it is not equal to a file # file in the differences list. If it is in the differences list and it is not in the # source list then it should be deleted from the destination directory. 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 in 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