Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Clear questions and runnable code
get the best and fastest answer

how to know if a path exists or not

by Anonymous Monk
on Oct 11, 2012 at 14:54 UTC ( #998465=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a windows path like below,i want to know if this path exists or not,please suggest how can this be done in perl c:\data\logs

Comment on how to know if a path exists or not
Re: how to know if a path exists or not
by tobyink (Abbot) on Oct 11, 2012 at 15:11 UTC

    There is a group of built-in Perl functions to do this sort of thing (checking the existence of files, whether they're readable/writeable/executable, whether they're directories, files or something else, etc). This group of functions is unusual in that their names all begin with a hyphen; usually hyphens are not permitted in function names.

    if (-e "C:\\Data\\Logs") { print "it exists!\n"; } if (-d "C:\\Data\\Logs") { print "it is a directory!\n"; } if (-r "C:\\Data\\Logs") { print "it is readable!\n"; } if (-w "C:\\Data\\Logs") { print "it is writeable!\n"; }

    See the documentation for -X in perlfunc.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: how to know if a path exists or not
by kennethk (Monsignor) on Oct 11, 2012 at 15:13 UTC
    File test operations are cataloged in -X. In particular, -e will tell you if something exists at that path, and -f and -d will test if a given string corresponds to a file and a directory respectively.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      I would try to open the path and see if it exists or not

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-04-25 09:01 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (586 votes), past polls