Most likely your problem is that you try to reread using DIR without reopening it. However there are a number of things that will help to tidy your code up. Consider (untested):

#!/usr/local/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES ); my @testFiles = listFiles ('C:\Perl\test'); # while loop to try every min for 3 mins if any the file counts a & b +don't match my $retries = 0; while ($retries++ < 3) { my @compressFiles = listFiles ('C:\Perl\compress'); if (@testFiles == @compressFiles) { compress (\@compressFiles); unlink @compressFiles; #delete compressed files unlink @testFiles; #delete test files last; } print "Count Compare Failed!\n"; sleep 60; } sub listFiles { my ($dirName) = @_; chdir ($dirName) or die "Can't chdir to $dirName: $!"; opendir my ($scan), $dirName or die "Couldn't opendir $dirName: $! +\n"; return grep {$_ ne '.' && $_ ne '..'} readdir $scan; } sub compress { my @add_array = @{$_[0]}; my $obj = Archive::Zip->new (); # new instance $obj->addFile ($_) foreach @add_array; # add files if ($obj->writeToFileNamed ('testing.zip') != AZ_OK) { # write +to disk print "Error in archive creation!"; } else { print "Archive created successfully!"; } }

Note in particular that an array in scalar context provides the number of elements in the array so the test file count is simply @testFiles for example.

If you find yourself repeating more than a couple of lines of code consider using a sub instead. In this case listFiles not only makes the code more compact, but clears the flow up so it's easier to see where the error was.

Note that this code does not compare files, but only the number of file and directory entries in the subject folders. In particular, if files change names, but the number of files don't this code (probably) will not do what you want!

True laziness is hard work

In reply to Re: Deleting files in the first folder by GrandFather
in thread Deleting files in the first folder by sowais

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.