# copy.pl # Copy all .txt files from source directory, zip them and send the zipped file to destination directory use strict; use warnings; use diagnostics; use File::Find; my @textFiles; # array in which full pathnames are stored my $sourceDir = 'C:/sourceDirectory/One'; # Source directory my $destDir = 'C:/destinationDirectory/Two/'; # Destination directory # Finds files that match criteria (all .txt files) in sourceDir and adds them to array File::Find::find( \&add_to_array, $sourceDir ); sub add_to_array # subroutine that adds .txt files to the array { push @textFiles, $File::Find::name if ( /\.txt$/); # adds files to textFile array using File::Find::name if they end in .txt } # This while loop will be used to get rid of the directory names and adds just the filenames to the new array (newTextFiles) my $counter = 0; # initialize counter to zero my $num = @textFiles; # gets total number of elements in the textFiles array my @newTextFiles; # initialize new array in which just filenames will be stored my $position; # initialize variable that seperates the directory from the filename my $shortFileName; # initialize variable that gets the filenames # Loops through while loop the same number of times as there are elements in textFile array while ($counter < $num) { $position = rindex($textFiles[$counter],"/") + 1; # finds last occurance of '/' that seperates directories from filenames $shortFileName = substr($textFiles[$counter], $position); # extracts the filenames push @newTextFiles, $shortFileName; # adds the filenames (without the directory names) to the new array (newTextFiles) print "$newTextFiles[$counter]\n"; # prints just the filenames $counter++; # increments count by one until all files from old array are added to new array } # Zips the files in array use Archive::Zip qw( :ERROR_CODES :CONSTANTS); my $zip = Archive::Zip->new(); # new instance foreach my $zipFiles (@newTextFiles) { $zip->addFile($zipFiles); # add files } if ($zip->writeToFileNamed("$destDir/textFiles.zip") != AZ_OK) { print "Error creating archive!\n"; } else { print "Archive successfully created!\n"; print "@newTextFiles\n"; }