I am trying to get the names of all first level directories under given path.
I tried to use File::Find but had problems.
Can someone help me with that?
I am trying to get the names of all first level directories under given path.
I tried to use File::Find but had problems.
Can someone help me with that?
You could use File::Find for that. For example:
For each file found under
'.'
, this will call thewanted
subroutine. Inside the subroutine you can use-d
to check for a directory.File::Find:find
descends to all subdirectories in the tree below the directory specified.you can use find2perl to translate your find command to perl. See perldoc find2perl for more info.
Workaround of maxdepth: (reference from Randall)
Code:
output
I'm running ActivePerl 5.10.1 under Windows XP. If I wanted to get all the names of the directories under the root drive F. I would use the following code:
Well, this usually works because my folder names do not contain the dot. Otherwise it fails.
Okay, it seems that even my method works for my case, people would still downvote because it is faulty. So I'd have to use the official approach, the -d flag to check if a file is a directory:
The upgraded code:
This worked for me.
Use
opendir
and-d
.Use the
-d
file check operator: