How to see if a directory exists or not in Perl?

132,668

Use -d (full list of file tests)

if (-d "cgi-bin") {
    # directory called cgi-bin exists
}
elsif (-e "cgi-bin") {
    # cgi-bin exists but is not a directory
}
else {
    # nothing called cgi-bin exists
}

As a note, -e doesn't distinguish between files and directories. To check if something exists and is a plain file, use -f.

Share:
132,668
Nano HE
Author by

Nano HE

A newbie, China based; And I am new to programming. Work mostly with C#, C++, Perl & Php; Using OS: winxp; C#/C++: Visual Studio 2005/2008; Perl: Active Perl 5.10.1 + Komodo Edit 6; Php: NetBeans IDE 6.7 + XAMPP 1.7.3 Thanks for reading and for the replies! ;) I'm Reading Magic Tree House

Updated on December 22, 2020

Comments

  • Nano HE
    Nano HE over 3 years

    To see if a file exists before using it, we can use:

    if (-e "filename.cgi")
    {
     #proceed with your code
    } 
    

    But how to indentify a directory exists or not?