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";

In reply to Check for a new file in a directory by nfaber

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.