readdir
DIRHANDLE
This function reads directory entries from a directory handle opened by opendir . In scalar context, this function returns the next directory entry, if any, otherwise an undefined value. In list context, it returns all the rest of the entries in the directory, which will of course be a null list if there are none. For example:
opendir THISDIR, "." or die "serious dainbramage: $!"; @allfiles = readdir THISDIR; closedir THISDIR; print "@allfiles\n";
prints all the files in the current directory on one line. If you want to avoid the "
.
" and "
..
" entries, use this instead:
@allfiles = grep !/^\.\.?$/, readdir THISDIR;
And to avoid all
.*
files (like the
ls
program):
@allfiles = grep !/^\./, readdir THISDIR;
To get just text files, say this:
@textfiles = grep -T, readdir THISDIR;
But watch out on that last one, because the result of readdir needs to have the directory part glued back on if it's not the current directory - like this:
opendir THATDIR, $thatdir; @text_of_thatdir = grep -T, map "$thatdir/$_", readdir THATDIR; closedir THATDIR;