Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Getting a simple directory listing

by Stamp_Guy (Monk)
on May 01, 2001 at 04:04 UTC ( [id://76826]=perlquestion: print w/replies, xml ) Need Help??

Stamp_Guy has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to get a simple listing of all the sub folders (but not files) in a certain directory. I can't figure for the life of me what's wrong with this code. I put in the Cwd thing to make sure the path was correct, and it is, but I just keep getting a result of "..." which I understand is a combination of "." and ".." but it doesn't list any subdirectories. The folder DEFINATELY has subdirectories, and I'm running this on a Win95 machine. Any ideas? Here's my code:

use Cwd; print "Current dir is: ", cwd, "\n"; local $_; opendir(TEXTFILES, "desktop") || die "Couldn't open the text file dire +ctory: $!"; @data=readdir(TEXTFILES); foreach (@data) { print if(-d "./$_"); } closedir(TEXTFILES);

Replies are listed 'Best First'.
Re: Getting a simple directory listing
by ZZamboni (Curate) on May 01, 2001 at 04:09 UTC
    The problem is that you are opening the directory "desktop" for reading, but you are testing each entry under the current directory. Changing the following line makes it work:
    print if (-d "./desktop/$_");

    --ZZamboni

      Thanks everyone. ZZamboni's suggestion worked perfectly.
Re: Getting a simple directory listing
by tinman (Curate) on May 01, 2001 at 04:12 UTC

    Your code works for me, after I made a simple substitution of opendir(TEXTFILES, ".") || die "Couldn't open the text file directory: $!"; instead of trying to open directory "desktop".. You need to change to the "desktop" directory, because you're doing the test on the current directory..
    HTH

    The full code that worked for me follows:

    use Cwd; print "Current dir is: ", cwd, "\n"; local $_; opendir(TEXTFILES, "c:\\CA_LIC") || die "Couldn't open the text file d +irectory: $!"; @data=readdir(TEXTFILES); foreach (@data) { print $_, "\n" if(-d "C:\\CA_LIC\\$_ "); } closedir(TEXTFILES);

    Update:ZZamboni types faster than I do :o), obviously, he's said what I've tried to say too, better, in fact :o)
      Ok, ZZamboni, I tried that and it works. I am just trying to get an array with all the subdirectories of the current dir, and this doesn't work:
      use Cwd; print "Current dir is: ", cwd, "\n"; opendir(TEXTFILES, "desktop") || die "Couldn't open the text file dire +ctory: $!"; @data=readdir(TEXTFILES); foreach (@data) { push (@dir) if (-d "./desktop/$_"); } closedir(TEXTFILES); print @dir; # This line is just here to make sure it worked, which it +currently doesn't.
      Update: Is there a more efficient way to do this? This seems a bit bulky. If someone could show me a better way (and explain it for this newbie) to do this, I'd really appreciate it.
        push does not use $_ as an implicit argument, so when you do push(@dir) you are not pushing anything into @dir. You have to do:
        push(@dir, $_) if (-d "./desktop/$_");
        Although I would substitute that whole block (the readdir and the foreach) by:
        @dir = grep { -d "./desktop/$_" } readdir(TEXTFILES);
        :-)

        --ZZamboni

Re: Getting a simple directory listing
by eejack (Hermit) on May 01, 2001 at 04:14 UTC
    Howdy Stamp Guy, On my windows 98 machine your script worked fine when I specified the full path of the desktop ie.
      opendir(TEXTFILES, "c:/windows/desktop") || die "Couldn't open the text file directory

    When I didn't do that, die kicked in and gave me your warning.

    HTH,

    Update Okay, so everyone types faster than I do..:) Another thing you might want to do is

    $dir = "c:/windows/desktop"; chdir $dir or (warn "Cannot chdir $dir: $!" and next); opendir(TEXTFILES, "$dir") || die "Couldn't open the text file directo +ry

    Granted that is a bit pedestrian, but I like it when it is obvious.

    EEjack

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://76826]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found