-d and Find::File::name

Posted on 2003-02-26
Medium Priority
Last Modified: 2012-05-04
   I have this snippet of code in which I am trying to print out all the directories. It does not work using the -d, but -f does print all the files. Why doesn't -d work for directories? What am I doing wrong? This should be a pretty basic question for an experienced Perl programmer.


use strict;
use File::Find;

sub wanted
   if (-d $File::Find::name)
      print "$File::Find::name\n";

find(\&wanted, 'C:/');


many thanks,


Question by:titanandrews
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 8030818
use strict;
use File::Find;

sub wanted
   print "$File::Find::name\n" if -d;

find(\&wanted, 'C:/');

Accepted Solution

PC_User321 earned 200 total points
ID: 8032526
>>  What am I doing wrong?

Your program works for -f and for -d.
But if you specify a relative path (eg '.') instead of the absolute 'c:/' then the program does not work as expected.  The reason is that 'wanted' changes to the current directory as it tunnels through the directory structure.
So if the program started in '.' (which happens to be c:/) then when it gets to WinNT and asks "Is ./WinNt/System32 a valid directory" the answer will obviously be 'No'.

If you change your 'if' line to
   if (-d $_)
(which if equivalent to 'if (-d)')  
then everything will work because, for example, when in WinNT the question will be "Is System32 a valid directory", and the answer will obviously be 'Yes'.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question