Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Check for a new file in a directory

by nfaber (Initiate)
on Apr 21, 2004 at 15:36 UTC ( [id://347034]=perlquestion: print w/replies, xml ) Need Help??

nfaber has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Check for a new file in a directory
by insensate (Hermit) on Apr 21, 2004 at 15:52 UTC
    You're populating @difference with files in the directory that aren't in the log, as well as files in the log that aren't in the directory. Hence deleted files are showing up...
    foreach my $logName (keys %loglist) { unless (defined $dirlist{$logName}) { push @difference, "$logN +ame";} }
    May be omitted.
      Thank you insensate! It worked. Leave it to the monks.
Re: Check for a new file in a directory
by AcidHawk (Vicar) on Apr 22, 2004 at 07:40 UTC
    Have a look at Roth's DirMon script which is a dir monitor script running as a windows service.

    YMMV

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://347034]
Approved by insensate
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found