Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Searching directories

by LazerRed (Pilgrim)
on Sep 23, 2003 at 01:48 UTC ( #293358=note: print w/replies, xml ) Need Help??

in reply to Searching directories

I do not have a D: on this win box, but maybe modification will help you see what is going on. I run my version from C:/ and get the desired results.

my $dirs2 = "C:/WINNT"; opendir $dirs2, "C:/WINNT" or die "Could not open directory: $!"; while ($_ = (readdir $dirs2)) { if (-d "$dirs2/".$_) { print "Directory: $_\n"; } }

What's happining with your script is:

You are reading the contents of D: while running the script from C:
Your list of files are created, then you want to check if they are directories. Your directory check is being performed with a C: path from the files read in from D: because "readdir" does not include the absolute pathname of a file. So, unless you have a directory on D: that is the exact same as what is on C: nothing will be printed.

Hope this helps.


Update: For clarity, you need to provide a fully qualified path to your directory check, because perl will default to the current directory it was run from if you do not. (or use a cwd)

Whip me, Beat me, Make me use Y-ModemG.

Replies are listed 'Best First'.
Re:Searching directories
by pen (Acolyte) on Sep 23, 2003 at 13:32 UTC
    This is allot of help LazerRed:)
    I was wondering if this could be what it was, and was looking at ways of biulding the path but just couldn't get it right at all. Still allot to learn at this point.

    As for the other two suggestions thanks but I think they are a little ahead of me at this point. I will keep them in mind and come back to it at some point.

    I now have what I want but instead of using openDir I am now using glob. Is this a good way to go for a recursive sub?

    Thanks again
    Paul Neale

      Is this a good way to go for a recursive sub?
      Generally accepted answer: no. Use File::Find, which is a standard module, or maybe File::Find::Rule, which many people seem to prefer — I don't know it personally, but I thought I'd mention it anyway, otherwise there is bound to be a follow-up to this post pointing to it. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://293358]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-07-21 13:42 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (449 votes). Check out past polls.