in reply to Notification of a new file added to directory

Why not use the -M operator to test for the age of the file (result in days). So something like:

sub test_new { my ($mydir) = @_; my $new_one = 0; opendir(DIR, $mydir) or die("..."); while (my $file = readdir(DIR)) { if ($file !~ /^\.+$/ && -M $mydir . '/' . $file < 0.5) { $new_one = 1; last; } } closedir(DIR); return $new_one; }
would do.

Hope this helps, -gjb-

Update: Stupid me! I tested the script only in the current working directory, obviously the file test should read -M $mydir . '/' . $file < 0.5 rather than -M $file < 0.5 which looks for the file in the script's directory. (modified the original code already). Incidently, the error is easily caught with use warnings; since a warning about the use of an uninitialized value in numeric lt is issued.

Update: Added code to filter out the . and .. directories that were tested as well.

Replies are listed 'Best First'.
Re: Re: Notification of a new file added to directory
by Jenda (Abbot) on Jan 03, 2003 at 18:07 UTC

    While this will work fine if the files are CREATED there, it may fail badly it they are MOVED. At least if they are moved within one logical disk.

    I'd use a hash tied to a DBM containing the last modification dates of all files in the directory and processed all that 1) were not present or 2) have a different modification time.

    use DB_File; tie %ages, 'DB_File', $age_database, O_CREAT|O_RDWR, 0700; sub test_new { my ($mydir) = @_; my $new_one = 0; opendir(DIR, $mydir) or die("..."); while (my $file = readdir(DIR)) { next if $file !~ /^\.+$/; my $mtime = (stat($file))[9]; next if $mtime == $ages{$file}; $ages{$file} = $mtime; #the $file is new! Process it any way you like $new_one = 1; } closedir(DIR); return $new_one; }

    If all you need to know is whether there was any change then maybe it would be better to generate an MD5 hash of the directory contents (filenames + their modification times) and compare the result with a stored value.

    Jenda

Re: Re: NOtification of a new file added to directory
by Anonymous Monk on Jan 03, 2003 at 15:26 UTC
    I tried this and it seems to return "true" (1 value) on my directory even though last added file is a week old. Please advise what I am doing wrong?
    #!/usr/local/bin/perl use strict; my $mydir = "/mydirectorypath/directory"; my $new_one = 0; opendir(DIR, $mydir) or die("..."); while (my $file = readdir(DIR)) { if (-M $file < .5) { $new_one = 1; last; } } closedir(DIR); print "$new_one\n";

      I modified the original node. Sorry for the confusion, it was silly on my part, -gjb-

        Thanks for your patience and time! It now works and I learned alot from this.
Re: Re: NOtification of a new file added to directory
by Anonymous Monk on Jan 03, 2003 at 16:20 UTC
    It still returns a "1" on my directory even if files are older than a day. Is there something I am doing wrong here???
    #!/usr/local/bin/perl use strict; my $mydir = "/mypath/directory"; my $new_one = 0; opendir(DIR, $mydir) or die("..."); while (my $file = readdir(DIR)) { if (-M $mydir . '/' . $file < 0.5) { $new_one = 1; last; } } closedir(DIR); print "$new_one\n";

      I updated the code again, see if this helps.

      Incidently, why not register, it's much easier to notify you of a change using the chatterbox rather than having to post these otherwise useless notes.

      Hope it works for you this time, -gjb-