Hello All.
I have the following script that opens up a directory, gets a listing of the files and writes it to a logfile. The next time it runs, it opens up the logfile, writes it into an array, opens the directory again and compares it with the directory listing the last time the program was run.
This script is supposed to print out the name of any NEW files in a direcotry, however it also pribts out the name of any files that are DELETED from the directory. I do not want this. I only want it to print out the name of any NEW file, or at least print out something like
xxxx.txt is a new file
or
xxx.txt was deleted.
This is a little beyond me oh great monks. Can I get some help?
MY CODE:
#!D\perl\bin\perl.exe -w
use strict;
my (%dirlist, %loglist, @difference, $dir);
unless ($dir = $ARGV[0]) { die "Needs a directory.";}
$dir =~ s(\\+)(/)g;
unless (-d $dir) {die "This is not a valid directory. $!";}
my $logfile = "${dir}/mydirlog.txt";
opendir DIR, "$dir";
while ($_ = readdir(DIR)) {$dirlist{$_}++;}
closedir DIR;
if (-e $logfile) {
open (OLDLOG, $logfile) or die "Cannot open existing log file.\n$!
+";
while (<OLDLOG>) {chomp; $loglist{$_}++;}
close OLDLOG;
foreach my $dirName (keys %dirlist) {
unless (defined $loglist{$dirName}) { push @difference, "$dirN
+ame";}
}
foreach my $logName (keys %loglist) {
unless (defined $dirlist{$logName}) { push @difference, "$logN
+ame";}
}
}
open LOG, "> $logfile";
foreach (keys %dirlist) { print LOG "$_\n"; }
close LOG;
print "@difference";