Dear Perl Monks,

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.

# 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"; }
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.

In reply to Zipping an array problem by mountaingoat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.