Hello Monks! I have written a program to compare the file counts between two folders and once matched the files in the second folder are zipped and the ones in the first folder are deleted. My program functions as expected except for the deleting of files in the second folder. Could you please help. This is what I have so far

#!/usr/local/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES ); my $directory1 = 'C:\Perl\test'; chdir($directory1) or die "Can't chdir to $directory1 $!"; opendir(DIR1, $directory1) || die "Couldn't opendir: $!\n"; my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR1; my $a =0 ; foreach(@files){ $a++; } my $directory = 'C:\Perl\compress'; chdir($directory) or die "Can't chdir to $directory $!"; opendir(DIR, $directory) || die "Couldn't opendir: $!\n"; my @files2 = grep { $_ ne '.' && $_ ne '..' } readdir DIR; my $b=0; my $c=0; foreach(@files2){ $b++; } # while loop to try every min for 3 mins if any the file counts a & b +don't match while($c < 3) { if ($a eq $b) { compress(\@files2); unlink @files2; #delete files in the compress folder after + compression is complete unlink @files; #delete files in the test folder; $c=3; } else { print "Count Compare Failed!\n"; sleep 60; @files2 = grep { $_ ne '.' && $_ ne '..' } readdir DIR; $b=0; } $c++; foreach(@files2){ $b++; } } # subroutine to compress the files into one sub compress { my @add_array=@{ $_[0] }; my $obj = Archive::Zip->new(); # new instance foreach (@add_array) { $obj->addFile($_); # add files } if ($obj->writeToFileNamed('testing.zip') != AZ_OK) { # write to di +sk print "Error in archive creation!"; } else { print "Archive created successfully!"; } } closedir(DIR1); closedir(DIR);

In reply to 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.