Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

How to determine whether a dir entry is a directory?

( #106956=categorized question: print w/ replies, xml ) Need Help??
Contributed by Codmate on Aug 22, 2001 at 18:20 UTC
Q&A  > directories


Description:

I'm using File::stat to get each entry's mode. It returns lots of numbers whose meanings are a mystery to me. Where can I find out more about this? I either need to know what these mode numbers mean, or need a better way of testing for files and directories.
opendir FOLDERS, $path or die "opendir $path - $!"; for ( grep /[a-z]/, readdir FOLDERS ) { my $st = stat( $path.$_ ); my $mode = $st->mode; print $mode; } closedir FOLDERS;

Answer: How to determine whether a dir entry is a directory?
contributed by ozone

you can simply use the -f and -d flags to determine if something is a file or a directory, like so:

my $fileA = "./something"; my $fileB = "./something/else.txt"; if(-d $fileA and -f $fileB) { ... }
This will check to see if $fileA refers to a directory and if $fileB refers to a file...
Answer: How to determine whether a dir entry is a directory?
contributed by je44ery

sub readin_dirs { my $dir = shift; my $DIR; # I use variable file handles so function can be reentrant opendir $DIR, $dir or die "opendir $dir - $!"; my @entries = readdir $DIR; # Get only directories from dir listing. my @subdirs = grep { -d "$dir/$_" } @entries; # Remove "hidden" directories (including . and ..) from that list: @subdirs = grep { !/^\./ } @subdirs; for my $subdir ( @subdirs ) { now_do_something("$dir/$subdir"); } closedir $DIR; }
Answer: How to determine whether a dir entry is a directory?
contributed by RAS230

You can use -d, but you have to be sure you're testing the correct pathname. Note that readdir returns unqualified names relative to the directory they're in.

my $dir = "/anydir"; opendir DIR, $dir or die "opendir $dir : $! \n"; for ( readdir DIR ) { my $dirent = "$dir/$_"; if ( -d $dirent ) { print "$dirent is a directory\n"; } } closedir DIR;
Answer: How to determine whether a dir entry is a directory?
contributed by I0

Fcntl has defines some symbols which can help here:

use Fcntl ':mode'; $mode & S_IFDIR; S_ISDIR($mode);

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (10)
    As of 2014-04-18 13:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (468 votes), past polls