Re: directory exists
by Zaxo (Archbishop) on Jun 03, 2005 at 00:05 UTC
|
What you have should tell you if something called /mydir is in the root fs. To see if it is a directory call -d.
my $mydir = '/mydir';
if ( -e $mydir and -d _ ) {
print $mydir, ' directory exists.', $/;
}
The underscore handle makes -d use the cached stat call from -e.
| [reply] [d/l] |
|
|
Isn't testing for -e and -d redundant? -d includes a -e test.
| [reply] |
|
|
| [reply] [d/l] |
|
|
is_dir {
-e shift() ?
-d _ ? 1 : 0
: undef;
}
With that I can test truth to see if there is a directory of that name, or defined to see if the name is in use.
| [reply] [d/l] |
|
|
|
|
Hello,
thank u for the very fast reply. I tried the syntax you posted but it doesnt work for me ..for some reason.
my $dev = '/dev';
if (-e $dev && -d _) {
# print "Directory exists \n";
print $dev, ' directory exists.', $/;
}
but If I use
perl -wle '$x=q(/dev); print "OK" if -d $x'
, it works fine from the command line
| [reply] |
|
|
$ perl -e'my $dev = "/dev";if (-e $dev and -d _) {print $dev," directo
+ry exists.",$/}'
/dev directory exists.
$
| [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
|
|
Hello everyone,
THANK YOU FOR ALL YOUR REPLIES...THE SYNTAX YOU SUGGESTED WORKED ONCE I CHANGED A FEW LINES OF CODE IN ANOTHER PLACE.....
thank you very much
| [reply] |
|
|
Hello, thank u for the very fast reply. I tried the syntax you posted but it doesnt work for me ..for some reason.
my $dev = '/dev';
if (-e $dev && -d _) {
print $dev, ' directory exists.', $/; }
but If I use
perl -wle '$x=q(/dev); print "OK" if -d $x' , it works fine from the command line
| [reply] |
Re: directory exists
by tlm (Prior) on Jun 03, 2005 at 00:06 UTC
|
I'm surprised it doesn't work (assuming, of course that /mydir indeed exists). Is this a Unix system?
Incidentally, I would use the -d test in this case, which implies -e, and returns true only if the tested file not only exists, but is actually a directory. For example (OS: Linux):
% perl -wle '$x=q(/boot); print "OK" if -d $x'
OK
| [reply] [d/l] |
|
|
| [reply] |
Re: directory exists
by jweed (Chaplain) on Jun 03, 2005 at 00:08 UTC
|
What is your expected behavior? It seems to work for me, though I would make the condition: ...
if (-e $mydir && -d _){
...
to explicitly test that the file is a directory.
| [reply] [d/l] |