Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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 (Abbot) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2015-07-06 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls