in reply to Re: run program until directory is not empty
in thread run program until directory is not empty

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;

Replies are listed 'Best First'.
Re^3: run program until directory is not empty
by onelesd (Pilgrim) on Jun 15, 2011 at 20:34 UTC
    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.