Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Issue with IsDirectory -d

Reply
Thread Tools

Issue with IsDirectory -d

 
 
Ant
Guest
Posts: n/a
 
      05-10-2006
Hi

Has anyone encountered an issue with using the Perl IsDirectory -d?

For example, the following snippet of code should yield 3 invalid
directory responses.

my $folderExists = -d "C:Foo";
my $folderExists2 = -d "C:\\Foo";
my $folderExists3 = -d "";

None of the above folders/directories exist (using XP Pro) yet the
result of $folderExists is 1.
The others yield undefined values which is/should be correct.

Has anyone encountered this or is C:Foo being interpreted as something
else (my only assumption - like C: on its own).

Ant

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-10-2006
"Ant" <> wrote in news:1147267243.388953.307580
@i39g2000cwa.googlegroups.com:

> For example, the following snippet of code should yield 3 invalid
> directory responses.
>
> my $folderExists = -d "C:Foo";
> my $folderExists2 = -d "C:\\Foo";
> my $folderExists3 = -d "";
>
> None of the above folders/directories exist (using XP Pro) yet the
> result of $folderExists is 1.


Please post a short but complete script along with its output when you
post code. It is hard to tell what you are printing and how.

> The others yield undefined values which is/should be correct.


All of the various methods below give the expected results for me
(c:/opt exists and is a directory on my system).

#!/usr/bin/perl

use warnings;
use strict;

use Data:umper;

my @dirs = ('C:Foo', 'C:/Foo', q{}, 'C:/opt');

{
my @flags = map { -d $_ ? 1 : 0 } @dirs;
print "$_\n" for @flags;
}

{
my @flags = map { -d $_ } @dirs;
print Dumper \@flags;
}

{
my @flags;

for my $i ( 0 .. $#dirs ) {
$flags[$i] = -d $dirs[$i];
}

print Dumper \@flags;
}

{
my $e1 = -d 'C:Foo';
my $e2 = -d 'C:/Foo';
my $e3 = -d q{};

print "$_\n" for $e1, $e2, $e3;
}
__END__

> Has anyone encountered this


No.

> or is C:Foo being interpreted as something
> else (my only assumption - like C: on its own).


C:Foo is *NOT* the same as C:\Foo. I think C:Foo refers to something
called Foo in the current directory on drive C which, in your case,
seems to have a subdirectory called Foo.

Or, there is something else going on, but it is hard to know because you
did not show the exact code you ran.

For example:

D:\Home\asu1\UseNet\clpmisc> cat ddd.pl
#!/usr/bin/perl

use warnings;
use strict;

my @dirs = ('C:Foo', 'C:/Foo', q{}, 'C:/opt');

{
my @flags = map { -d $_ ? 1 : 0 } @dirs;
print "$_\n" for @flags;
}

__END__

D:\Home\asu1\UseNet\clpmisc> ddd
1
0
0
1

D:\Home\asu1\UseNet\clpmisc> c:

C:\opt\psdk> dir | grep Foo
05/10/2006 09:45 AM <DIR> Foo


--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
 
 
 
Ant
Guest
Posts: n/a
 
      05-10-2006
ok, apologies for not being clearer.

The code I ran is complete as originally posted minus some print
statements for each $folderExists*.
I get a 1 for C:Foo and 2 undefinds.
I did say that none of the folder C:Foo C:\\Foo or "" exist on my drive
so all should yield undefined should they not.
The code snippet you have posted is a alot more detailed than mine but
all, I guess, I am after is why does -d return different results when
all the folders/directories do not exist?.

Ant

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-10-2006
"Ant" <> wrote in news:1147269547.725215.95260
@u72g2000cwu.googlegroups.com:

> ok, apologies for not being clearer.


Please quote some context when you reply.

> The code I ran is complete as originally posted minus some print
> statements for each $folderExists*.


Post the exact code you used and the output. Nothing minus, nothing
edited.

Please do read and follow the posting guidelines. That will maximize
your chances of solving your problem.

> I get a 1 for C:Foo and 2 undefinds.
> I did say that none of the folder C:Foo C:\\Foo or "" exist on my
> drive so all should yield undefined should they not.


Did you read my response completely? As I pointed out C:Foo is not
determinate. Which file location it refers to depends on what the
current directory on drive C: is when your script runs.

> The code snippet you have posted is a alot more detailed than mine but
> all, I guess, I am after is why does -d return different results when
> all the folders/directories do not exist?.


There is nothing wrong with -d: It does not indicate a directory exists
when it does not. C:Foo does exist on your system. It is just not the
location you think it is.

Post the *EXACT* code you ran.

#!/usr/bin/perl

use warnings;
use strict;

use File::Spec::Functions qw( rel2abs canonpath );

my $dir = 'C:Foo';

printf "%s => %d\n", canonpath(rel2abs $dir), -d $dir;

__END__

D:\Home\asu1\UseNet\clpmisc> ddd
C:\opt\psdk\Foo => 1

D:\Home\asu1\UseNet\clpmisc> c:

C:\opt\psdk> cd \

C:\> d:

D:\Home\asu1\UseNet\clpmisc> ddd
Use of uninitialized value in printf at D:\Home\asu1\UseNet\clpmisc
\ddd.pl line 10.
C:\Foo => 0

Sinan

--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
Ant
Guest
Posts: n/a
 
      05-10-2006
>>Post the exact code you used and the output. Nothing minus, nothing
>>edited.


There is no need now, your response at the end hit the nail on the
head. Thank you Sinan.

>>C:Foo does exist on your system. It is just not the
>>location you think it is.


Agreed and this is what I have missed. The old cwd issue, damn it!. I
missed this totally and its such as a basic thing to miss.

Thanks for your help Sinan.

Ant

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-10-2006
"Ant" <> wrote in news:1147272391.564395.22570
@j73g2000cwa.googlegroups.com:

>>>Post the exact code you used and the output. Nothing minus, nothing
>>>edited.

>
> There is no need now, your response at the end hit the nail on the
> head. Thank you Sinan.


You are welcome. Glad it helped.

And thank you for adopting a better follow-up style. However, I would ask
you to also include attributions when you reply.

Sinan
--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it a hardware issue or a config issue or something else Scooty Cisco 0 06-14-2008 04:02 PM
Data Storage Issue (Basic Issue) Srini Java 11 06-01-2008 01:17 AM
Service Pack 2: login issue's and power management issue's ?!? Skybuck Flying Windows 64bit 0 04-07-2007 03:12 PM
inspiron 8200 video issue and hd issue the pez lover Computer Support 1 02-05-2007 02:44 AM
Major ActiveX Domain issue. NOT LOCAL PC ISSUE joe.valentine@gmail.com Computer Support 8 02-06-2006 09:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57