checking existance of a directory

E

Eric Peterson

I'm looking for the equivalent of
if (-e "filename") { }
for checking existance specifically of a directory

Thanks,
-Eric
 
T

terry l. ridder

hello;

I'm looking for the equivalent of
if (-e "filename") { }
for checking existance specifically of a directory

man perlfunc

-d File is a directory.
 
L

Leonard Challis

I'm looking for the equivalent of
if (-e "filename") { }
for checking existance specifically of a directory

First:
http://www.google.co.uk/search?hl=en&q=check+a+directory+exists+perl&meta=

-e is a so called -X flag. On google there are pleny of links to
comprehensive lists of these.

To check if a file is a directory, use the -d flag. e.g:

Plenty of examples are on the first Google result here:
http://www.developer.be/forums/index.cfm/fuseaction/dsp_full_thread/fullthreadid/355/forumID/5.htm

Personally, something like

| mkdir("c:\temp", 0777) unless (-d "c:\temp");

looks quite nice to me :D

Lenny
 
J

jl_post

Eric said:
I'm looking for the equivalent of
if (-e "filename") { }
for checking existance specifically of a directory


Use the "-d" call, like this:

if (-d "directory") { }

(Note that "-e" only checks to see is "filename" exists, but it does
not verify that if "filename" is an actual name of a file (it could be
the name of a directory).)

To check for the existance of a file, use "-f", like this:

if (-f "filename") { }

There are a lot more call like "-e", "-f", and "-d" that you should
probably know about. You can read about them by typing
"perldoc -f -e" at the command line.
I hope this helps, Eric.

-- Jean-Luc
 
L

Leonard Challis

A. Sinan Unur said:
1. Did you really want to create a directory whose name contains a tab
character?

Thanks for pointing that one out. Programming in different languages besides
Perl really can mess your syntax up sometimes hehe :D
2. There is no guarantee that a directory or file will not spring to
existence between the check for its existence and your attempt to create.
Therefore, you are better off with just attempting to create it straight
away.

That's interesting. I would have thought that it would be less than a split
second to check AND create the file. Is there really a possibility? Do you
have any examples? Thanks for the tip I'll bear that one in mind.

Are you saying something such as the following would be more "correct" in
your views?

| if (mkdir "dirname") { # created successfully }
| else { #not made }

or something similar?

Lenny
 
A

A. Sinan Unur

That's interesting. I would have thought that it would be less than a
split second to check AND create the file. Is there really a
possibility?

There is a good discussion about this in "Programming Perl", 3rd ed p.
571. I remember seeing the same discussion quite a few times in other
contexts but I only got it after reading Larry Wall et al.'s
description.
Do you have any examples? Thanks for the tip I'll bear
that one in mind.

It creates a race condition. While I cannot concoct a demonstration,
when a race condition is theoretically possible, you can be sure it will
bite you at some point.
Are you saying something such as the following would be more "correct"
in your views?

| if (mkdir "dirname") { # created successfully }
| else { #not made }

Exactly.

Sinan.
 
M

Martin Kissner

A. Sinan Unur wrote :

Isn't the above somehow different from this:

mkdir("c:/temp", 0777) unless (-d "c:/temp");

Besides the problem of the race condition this will not give you any
feedback if there exist a regular file "c:/temp" so I guess even

mkdir("c:/temp", 0777) unless (-e "c:/temp");

would be "better".
 
A

A. Sinan Unur

A. Sinan Unur wrote :

Isn't the above somehow different from this:

mkdir("c:/temp", 0777) unless (-d "c:/temp");

It _is_ different and that is why it is preferable.
Besides the problem of the race condition this will not give you any
feedback if there exist a regular file "c:/temp" so I guess even

mkdir("c:/temp", 0777) unless (-e "c:/temp");

would be "better".

No. You do bring up an important point, the race condition still exists in
this case and therefore it should not be used.

Sinan.
 
T

Tassilo v. Parseval

Also sprach Martin Kissner:
A. Sinan Unur wrote :

Isn't the above somehow different from this:

mkdir("c:/temp", 0777) unless (-d "c:/temp");

Besides the problem of the race condition this will not give you any
feedback if there exist a regular file "c:/temp" so I guess even

mkdir("c:/temp", 0777) unless (-e "c:/temp");

would be "better".

Still not good enough. 'mkdir' is one of the rare circumstances in which
you probably have to check the value of $! if the call fails:

use Errno qw/EEXIST/;

mkdir "c:/temp" or do {
if ($!{EEXIST}) {
die "Path is a file\n" if -f "c:/temp";
warn "No need to create existing directory\n"
} else {
die "Some other error: $!";
}
};

Tassilo
 
T

Tassilo v. Parseval

Also sprach Abigail:
A. Sinan Unur ([email protected]) wrote on MMMMCLX September
MCMXCIII in <URL:%%
%% It creates a race condition. While I cannot concoct a demonstration,
%% when a race condition is theoretically possible, you can be sure it will
%% bite you at some point.

Yeah, it creates a race condition.

You know what? I've *never* written a program where I care about it.
I happily write:

-d $dir or mkdir $dir or die;

Sure, there's the possibility some other program creates the directory
between checking for its existance and the attempt to create it. But
if I were to worry about that, I would also need to worry about a program
*deleting* the directory after my program created it.

Also people should note that if race conditions were a problem with
directories, then surely there'd be an intrastructure such as

mkdir $dir, O_EXCL or die $!;

along the line of using sysopen to create a file only when it doesn't
exist. But there isn't and there is simply no way to create a directory
and check for its success without this purely technical race condition.

The reason is that directories, unlike files, can be used by several
processes at the same time without the danger of data corruption. It's
files within a directory that have to be shielded against race
conditions, not the directory itself.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top