Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998465]
Approved by tobyink
and a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2017-10-23 21:47 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (285 votes). Check out past polls.