Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Think about Loose Coupling
 
PerlMonks  

get directory listing

by darrengan (Sexton)
on Nov 08, 2005 at 06:50 UTC ( #506624=perlquestion: print w/ replies, xml ) Need Help??
darrengan has asked for the wisdom of the Perl Monks concerning the following question:

HI dudes, i am trying to get the folder name from a directory. I am currently using the script below to get the content of the directory:
opendir(DIRHANDLE,"/usr/darren") || die "file to open"; @FILES = grep !/^\.\.?$/, readdir DIRHANDLE; while (<@FILES>) { chop(); $REC = $_; @LINEREC = split(/\./,$REC); print "$REC\n"; } closedir(DIRHANDLE);
By using the above method, will will get all the listing including the directory and files. how can i only show the directory in /usr/darren/?

Cheers,
Darren

Comment on get directory listing
Download Code
Re: get directory listing
by pg (Canon) on Nov 08, 2005 at 07:09 UTC

    Use -d to determine whether it is a directory. By the way, don't chop(), but chomp() if it is needed. The following code was tested on Windows XP, don't have UNIX access handy:

    opendir(DIRHANDLE,"c:/") || die "file to open"; for my $file (readdir DIRHANDLE) { print "$file\n" if (-d "c:/$file"); } closedir(DIRHANDLE);
Re: get directory listing
by jesuashok (Curate) on Nov 08, 2005 at 07:22 UTC
    Hi,

    even in Unix and linux, checking the file types as -d will go fine. Check the manual page of perlvar.

    getting all the file types.

    for example

    -f regular file -l sym.link file -s socket file

    "Keep pouring your ideas"
Re: get directory listing
by blazar (Canon) on Nov 08, 2005 at 11:15 UTC
    opendir(DIRHANDLE,"/usr/darren") || die "file to open";

    "file to open"?!? How 'bout $! anyway? (Check perldoc perlvar.)

    Whatever, I really wonder why people insist so much in reinventing the wheel where a simple glob would do.

    @FILES = grep !/^\.\.?$/, readdir DIRHANDLE;
    Is '.' fine? I mean: I'm not saying it is not. Simply generally people who reinvent the wheel do throw away it too...
    By using the above method, will will get all the listing including the directory and files. how can i only show the directory in /usr/darren/?

    -d is your friend. Check perldoc -f -X:

    my @dirs=grep -d, </usr/darren/*>; # Whoa it's one line!!
      Whatever, I really wonder why people insist so much in reinventing the wheel where a simple glob would do.

      That's because a simple glob will get you in trouble in production code. For example...

      my @dirs=grep -d, </usr/darren/*>; # Whoa it's one line!!

      Yeah it's on one line and looks elegant. Unfortunately there is no error control and it skips directories with a leading '.' in the name (e.g. '.foo', '..bar'). With readdir you need to be a little careful, but it works fine. If you aren't chdir'd into the directory you are reading you generally need to prepend the directory path to each name it returns, but that's trivial.

        Yeah it's on one line and looks elegant. Unfortunately there is no error control and it skips directories with a leading '.' in the name (e.g. '.foo', '..bar').
        1. From perldoc File::Glob:
          The POSIX defined flags for bsd_glob() are: "GLOB_ERR" Force bsd_glob() to return an error when it encounters a directory it cannot open or read. Ordinarily bsd_glob() continues to find matches.
        2. Skipping directories with a leading dot may be a feature. One can glob '.*' if need be.
        Not to say that there are not situations in which an explicit opendir & C. is actually preferrable. Indeed there are. My point being that in the vast majority of cases a simple glob would do instead. I think OTOH it has a stigma due to the fact that anciently it relied on a call to an external shell, which is what I read it in a book, but since I don't have effective experiennce with Perl 4, the last one that I think was set up like that, I cannot tell for sure...
Re: get directory listing
by merlyn (Sage) on Nov 08, 2005 at 11:33 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-04-17 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (443 votes), past polls