?
Solved

-d and Find::File::name

Posted on 2003-02-26
2
Medium Priority
?
186 Views
Last Modified: 2012-05-04
Hi,
   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.

<code>

use strict;
use File::Find;

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

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

</code>

many thanks,

Barry

0
Comment
Question by:titanandrews
[X]
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
2 Comments
 
LVL 48

Expert Comment

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

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

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

Accepted Solution

by:
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'.
0

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