Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

problem in checking if directory exists

by denzil_cactus (Sexton)
on Mar 31, 2008 at 07:30 UTC ( #677455=perlquestion: print w/ replies, xml ) Need Help??
denzil_cactus has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am getting problem in checking if any directory exists my code is

unless(-d $dir) { `mkdir -p $dir`; }
here I want to put the flag if the directory exists then no need to mkdir again but I am getting the following error

mkdir: cannot make directory: /usr/spool/uucppublic/sent/bki_prod: Fil +e exists (error 17) mkdir: cannot make directory: /usr/spool/uucppublic/sent/bki_prod: Fil +e exists (error 17)
even the directory exists.

suggest me if I am doing anything wrong here.

Thanks

Comment on problem in checking if directory exists
Select or Download Code
Re: problem in checking if directory exists
by Anonymous Monk on Mar 31, 2008 at 07:39 UTC
    perldoc -f mkdir
      Thanks for the reply Anonymous monk

      Well you didnt follow my question here.I am asking about the unless code

      unless(-d $dir) { }
      mkdir comand I know.
        mkdir comand I know.

        That's the point, you are not calling the Perl mkdir command, you are spawning a child process to run an external program called mkdir. The error is comming from the external program, not from perl.
Re: problem in checking if directory exists
by jwkrahn (Monsignor) on Mar 31, 2008 at 08:37 UTC

    Instead of using  `mkdir -p $dir` use the mkpath function in File::Path.

Re: problem in checking if directory exists
by moritz (Cardinal) on Mar 31, 2008 at 08:47 UTC
    "file exists" means that there is a file of the same name (but it's not a directory in your case).

    You can check with -e $dir if it exists, and remove it with unlink if it's an ordinary file.

      [denzil_cactus]: unless(-d $dir){ `mkdir $dir`;}is this code correct i +f I want to check the directory exists or not? [moritz]: use if (-d $dir) [ysth]: no, the -d could fail if you don't have access (but then you p +robably wouldn't be able to do the mkdir either [ysth]: and use perl's mkdir, not the shell [ysth]: mkdir [ysth]: err, I mean the -d could fail but the directory already exist [moritz]: yes, then use -e instead [moritz]: or whatever test you're feeling lucky with ;-) [ysth]: if you may need to create multiple levels of directory (say, c +reate foo/bar/baz where only foo exists), use File::Path::mkpath (in +an eval {} )
        So did it work that way? If not, what's your current code, what's the error message, and how does the directory look like?
Re: problem in checking if directory exists
by Anonymous Monk on Mar 31, 2008 at 10:46 UTC
    #!/usr/bin/perl use strict; use warnings; unless (-d '/some/user/directory') { mkdir '/some/user/directory'; } else { print "Directory exists", "\n"; }
Re: problem in checking if directory exists
by parv (Priest) on Mar 31, 2008 at 10:50 UTC
    Why not just invoke mkdir -p, or mkpath equivalent, instead of checking the existence before hand? Nothing is gained in doing that in two steps (unless somebody corrects me).
Re: problem in checking if directory exists
by roboticus (Canon) on Mar 31, 2008 at 12:35 UTC
    denzil cactus:

    One thought occurs to me: if you got $dir from the user and forgot to chomp it, you'll have a trailing EOL in it. The `mkdir -p $dir` won't care about it, but -d $dir might. (I've not tested it, so I'm not certain.) If that's the case, then -d would report that the directory "foo\n" doesn't exist even when "foo" does, causing mkdir to run each time you execute your code.

    ...roboticus

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-08-20 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (119 votes), past polls