in reply to problem in checking if directory exists

"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.

Replies are listed 'Best First'.
Re^2: problem in checking if directory exists
by denzil_cactus (Sexton) on Mar 31, 2008 at 09:04 UTC
    [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?
        I dont know what is the reason may be it was some privilege problem but the following code is working fine now

        $dir = "/usr/spool/uucppublic/book_prod"; unless(-d $dir){ `mkdir $dir`; }
        Thanks