readdir the directory name and file name separately?

D

Davy

Hi all,

I found readdir generate list contain ".", "..", "directory name",
"file name".
How can I readdir only the "directory name" and only the "file name"?

Thanks!
Davy
 
E

Eric Schwartz

Davy said:
I found readdir generate list contain ".", "..", "directory name",
"file name".
How can I readdir only the "directory name" and only the "file name"?

You can't. But you can ignore the files:

my $dirname="/path/to/dir";
opendir my $dirhandle, $dirname or die "Couldn't open [$dirname]: $!";

while(my $entry = readdir($dirhandle)) {
next unless -d "$dirname/$entry"; # perldoc -f -X
# perldoc -f readdir (2nd paragraph)

# do other stuff here
}

-=Eric
 
D

Davy

A. Sinan Unur said:
Have you considered reading the documentation for the function you are
using?

perldoc -f readdir
[snip]

Shall I use
#--------
opendir(DIR, $some_dir);
@dir_list = grep{-d "$some_dir/$_"} readdir(DIR);
@file_list = grep{-f "$some_dir/$_"} readdir(DIR);
closedir(DIR);
#--------

Thanks!
Davy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top