Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about readdir() 1

Status
Not open for further replies.

droid99

Programmer
Jul 6, 2004
5
US
I have a question about the readdir() function in Perl. I am using readdir() to read out the contents of a specified directory:

opendir (PICKUPDIR, $dir); # $dir is the directory path

while ($contents=readdir(PICKUPDIR))
{
print <<END;
$contents<br>
END

}

Here is my question: Is there a way to specify that readdir ONLY read out the contents that are directories (not files). The code above prints out the files as well as the directories.

Any help would be appreciated.

 
Use the file test operator -d to check if it's a directory:
Code:
opendir (PICKUPDIR, $dir);  # $dir is the directory path

while ($contents=readdir(PICKUPDIR))
{
# skip to the next if it's not a dir
next unless -d "$dir/$contents";
print <<END;
        $contents<br>
END

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top