Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to test if a directory exists?

by newbie00 (Beadle)
on Dec 25, 2001 at 00:43 UTC ( [id://134211]=perlquestion: print w/replies, xml ) Need Help??

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

Hello and Happy Holidays.

Is there a way to test whether a directory exists (analogous to testing if a file exists)?

The reference books I've checked aren't clear on this. They just mention how to open a dir using a filehandle. I've also searched this site and I have not found the answer, yet.

Thank you.
--newbie00

Replies are listed 'Best First'.
Re: How to test if a directory exists?
by grep (Monsignor) on Dec 25, 2001 at 00:54 UTC
    Use -e to check if a file/dir exists. Then -d to check if it is a directory. You do not really need the -e, but it's nice to know.
    #!/usr/bin/perl -w use strict; my $dir = '/etc'; if (-e $dir and -d $dir) { print "SPOON :)\n"; } else { print "spork :(\n"; }
    You should really pick up "Programming Perl" it is explained in there. It is also under 'perldoc -d'

    grep
    grep> cd pub 
    grep> more beer
    
Re: How to test if a directory exists?
by tachyon (Chancellor) on Dec 25, 2001 at 00:53 UTC

    This will work:

    $somedir = "c:/windows"; if (-d $somedir) { print "$somedir exists"; } else { print "$somedir does not exist!"; }

    If you have a look at perlman:perlfunc and find -X under the Functions for filehandles, files, or directories you will find a range of file tests that can be applied to files and dirs.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: How to test if a directory exists?
by ariels (Curate) on Dec 25, 2001 at 17:24 UTC
    -d pathname will test if pathname exists and is a directory. If you want more resolution, you can say
    if (-d $pathname) { # $pathname exists and is a directory # ... } elsif (-e _) { # $pathname exists, but is NOT a directory # ... } else { # $pathname doesn't even exist # ... }
    Note the use of the special pseudo-filehandle _: it's used to test the last filehandle tested. See perlfunc for more details of this.

    In any case, testing for existence of a file or a directory is often indicative of a bug. It is almost always better to try to get what you need in a single operation. For instance, instead of saying

    if (-d $d) { open FILE, "$d/$f" or die "Open failed"; } else { die "No directory $d"; }
    do just the open:
    open FILE, "$d/$f" or die "Open failed";

    Let the operating system make sure your path is OK. The first way is subject to an insiduous race condition: directory $d could be created between the test -d $d and the error message, and the error message would be wrong. This is not too bad; but if some more complex error handling were used, it would be likely to break.

    So try not to test if you can do something with -X; just try to do it. If you can't, the OS will let you know.

Re: How to test if a directory exists?
by Alex the Serb (Monk) on Dec 25, 2001 at 12:32 UTC
    to make it a whole here comes all of it (I think):

    -r File or directory is readable -w File or directory is writable -x File or directory is executable -o File or directory is owned by user -R File or directory is readable by real user, not effective user (di +ffers from -r for setuid programs) -W File or directory is writable by real user, not effective user (di +ffers from -w for setuid programs) -X File or directory is executable by real user, not effective user ( +differs from -x for setuid programs) -O File or directory is owned by real user, not effective user (diffe +rs from -o for setuid programs) -e File or directory exists -z File exists and has zero size (directories are never empty) -s File or directory exists and has nonzero size (the value is the si +ze in bytes) -f Entry is a plain file -d Entry is a directory -l Entry is a symlink -S Entry is a socket -p Entry is a named pipe (a "fifo") -b Entry is a block-special file (like a mountable disk) -c Entry is a character-special file (like an I/O device) -u File or directory is setuid -g File or directory is setgid -k File or directory has the sticky bit set -t isatty() on the filehandle is true -T File is "text" -B File is "binary" -M Modification age in days -A Access age in days -C Inode-modification age in days

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found