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

Hi folks, I'm trying to write a little scanner that checks a specific directory (no recursion necessary) for new files every 10 seconds, get their names and process them. Here's what I wrote so far:
$dir = "/home/sites/home/users/admin/test/";
opendir (DIR, $dir) or die "cannot opendir $dir";
{
  $time = substr(time, length(time)-1, 1);
  if ($time eq "0") {
     my @only_files = grep {-f "$dir/$_"} readdir(DIR);
     foreach my $file (@only_files) {
        &process_file ($file);
     }
  }
  redo;
}
closedir (DIR);

Replies are listed 'Best First'.
Re: Scanning directory for new files
by repson (Chaplain) on Dec 14, 2000 at 07:09 UTC
    Okay that code look good, just a couple of things I'd change...

    Instead of looping until the last character of time is 0 you could simply put sleep(10); just before the redo, making your aim clearer overall (and safer, for example if there are few files the loop may repeat within the same second thus repeating the processing immediately).

    Depending on how the files are written they may still be arriving when you process them. This is unlikely to be desireable so it would be better if you only process files if they have already been in existance for the previous loop or two. The methods of doing that are left as an exercise.

    The use of substr (though in above paragraphs I suggest another solution) is better written as substr(time,-1)