I'm new to Perl, and I'm trying to zip all of the text files in and below one directory. I then need to put that zip file into another directory.
It prints all of the elements in both arrays, but the second array contains the files that I need to zip (the second array - @newTextFiles). I run the script and get no errors, but the zipped file (textFiles.zip) that gets created in my destination directory contains nothing.
I'm using the second array to hold only the filenames.txt (with all of the directory names stripped off -- as the first array contains the full paths). I know that I'm doing something wrong, as well as not coding efficiently, but I'm new to Perl. I'm doing this all in Windows using ActiveState Perl 5.10.
This will eventually be used to copy thousands of .txt files from a Windows box into a MySQL db on a Linux box. Any help is appreciated.# copy.pl # Copy all .txt files from source directory, zip them and send the zip +ped 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 ad +ds 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 t +o 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 a +dds 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 textFi +les array my @newTextFiles; # initialize new array in which just filenames will + be stored my $position; # initialize variable that seperates the directory fr +om the filename my $shortFileName; # initialize variable that gets the filenames # Loops through while loop the same number of times as there are eleme +nts in textFile array while ($counter < $num) { $position = rindex($textFiles[$counter],"/") + 1; # finds las +t occurance of '/' that seperates directories from filenames $shortFileName = substr($textFiles[$counter], $position); # e +xtracts the filenames push @newTextFiles, $shortFileName; # adds the filenames (wit +hout the directory names) to the new array (newTextFiles) print "$newTextFiles[$counter]\n"; # prints just the filename +s $counter++; # increments count by one until all files from ol +d 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"; }
In reply to Zipping an array problem by mountaingoat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |