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

how to know if a path exists or not

by Anonymous Monk
on Oct 11, 2012 at 14:54 UTC ( [id://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 (Canon) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-25 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found