in reply to run program until directory is not empty

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re: run program until directory is not empty

Replies are listed 'Best First'.
Re^2: run program until directory is not empty
by zac_carl (Acolyte) on Jun 15, 2011 at 18:56 UTC
    You can use this ;
    my $dir = "/var/tmp/monk"; chdir("$dir") or die "$!"; opendir(BIN, $dir) or die "Can't open $dir: $!"; #Read Directory readdir DIR; # reads . readdir DIR; # reads .. if (readdir DIR) { #now there should be a file or folder print "it's not empty"; } else { print "it is empty"; } close DIR;
      use strict ; use warnings ; my $dir = "/var/tmp/monk"; chdir("$dir") or die "$!"; opendir(DIR, $dir) or die "Can't open $dir: $!"; sub task { my $entry = shift || 'n/a' ; # got an entry, do something print 'got one: ', $entry, "\n" ; } sub check { my $entry = readdir DIR ; while ($entry && ($entry eq '.' || $entry eq '..')) { $entry = readdir DIR ; } if ($entry) { task($entry) ; check() ; } else { close DIR ; # uncomment for a script you can daemonize #sleep 1 ; #opendir(DIR, $dir) or die "Can't open $dir: $!"; #check() ; } } check() ;

      P.S. This takes the previous example and makes it recursive which is what the OP asked for.