From as I understand your description, you want one file to be a running list of the new files that have arrived in the directory, while the second file will be a list of just the new ones at that moment. If this is the case, then the following will probably get you what you want.
#!/usr/bin/perl
use strict;
use warnings;
# emailsent.txt-will contain the name of all files for whom an alert h
+as been sent
# emailtogo.txt-will contain the name of all new files for whom an ale
+rt has to be sent
my $matchfile = 'emailsent.txt';
my $outfile = 'emailtogo.txt';
##########################
my $directory = 'C:\';
opendir(DIR, $directory);
my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR;
closedir(DIR);
# storing all file names in the folder in an array
foreach(@files){
print $_,"\n";
}
# Build list of old files for comparison
open my $ih, '<', $matchfile or die "Can't open file, $matchfile: $!";
my %oldFiles = map {chomp; $_ => 1} <$ih>;
close $ih;
# Append new files to total list
open my $oh_tot, '>>', $matchfile or die "Can't open file, $matchfile:
+ $!";
open my $oh_new, '>', $outfile or die "Can't open file, $outfile: $!";
# Comparing the array to the names in $matchfile
foreach my $file (@files) {
if (! $oldFiles{$file}) {
print $oh_tot "$file\n";
print $oh_new "$file\n";
}
}
close $oh_tot;
close $oh_new;
- Miller