in reply to Why can't I readdir multiple times in a while loop?

Jeff is right. the rewinddir function is the way to do it. The fixed code looks something like this:

opendir(MYDIR, $dirname); while (condition) #condition ~ 1, loop basically forever { my @mail = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR; foreach (@mail) { # do stuff # delete each file } undef(@mail); # @mail shuld be empty now rewinddir( MYDIR ); # so rewind the directory sleep(1); # give cpu a break } # more stuff... closedir(MYDIR);

This code works fine, but you have INDIR instead of MYDIR ( why? ).. either way change
my @mail = grep { $_ ne '.' and $_ ne '..' } readdir INDIR;
to
my @mail = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR;

- Have Fun, XDB19

Replies are listed 'Best First'.
(Guildenstern ) REx2: Why can't I readdir multiple times in a while loop?
by Guildenstern (Deacon) on Aug 24, 2000 at 22:54 UTC
    Should have been MYDIR all the way through. Dain bramage in my transposition (didn't feel like cut n paste).
    I tried the rewinddir, but at the next readdir it gave me the names of the files that used to be in the directory, but had been deleted in the previous pass through the loop.