in reply to reading directories

The way that your code is structured, the only file in the directory that will be analyzed is the first one, technically ".".

try this:
opendir(DIR, "/users/foo/") || die "Whoops!"; @bar = readdir(DIR); closedir(DIR) || warn "Unable to close DIR"; splice(@bar, 0, 2); #get rid of '.' and '..'
And now, @bar contains all of the file names in directory '/users/foo/'. You can then use a foreach loop or whatever to analyze the contents of that array line by line.

Hope that helps,
higle

Update: Feel free to omit the splice bit in the above snippet for possible portability concerns. (though, this bit has been an integral part of a batch process that has been running flawlessly on my HP/UX machine every day for the last year or so, the song may not be the same for other OS flavors). I thought I'd throw that in to clean up the data, but it's not necessary for the example. Tip o' the lid to merlyn.

Update++: In addition, my little pre-lunch break, thirty-second hack (indeed, it was hacked together in the few seconds I had before my coworkers left me to starve) has recently become the subject of another thread... It was brought up that it had absolutely no error-checking, and indeed used a dubious hack to snip the dot and dotdot out of the array. I've replaced my nasty for loop with a nice, tidy splice command, and added perfunctory error checking to the opendir and closedir commands. I omitted those at first because I assumed they were rhetorical in such an abstract example. Assumption is the mother of all... etceteras. :)

Replies are listed 'Best First'.
Re: Re: reading directories
by merlyn (Sage) on Sep 15, 2001 at 05:45 UTC
    opendir(DIR, "/users/foo/"); @bar = readdir(DIR); closedir(DIR); for (1..2) {shift @bar;} #get rid of '.' and '..'
    There is no promise that the first two elements returned from a readdir are dot and dotdot, so this code is dangerously unportable. It might not even work on your own machine tomorrow.

    -- Randal L. Schwartz, Perl hacker