Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > opendir and if -d $files problem

Reply
Thread Tools

opendir and if -d $files problem

 
 
Bob Gervais
Guest
Posts: n/a
 
      11-07-2003
Hi,

I am writing a very simple script that should return all
subdirectories on a given path (non recursively, so just first level).

This is the sub that I have for doing this but it gives very strange
results:

sub FindFiles
{
my ($dir) = @_ ;
my @files ;

opendir THISDIR, $dir or die "Error, cannot do opendir on $dir $!"
;
my @allfiles = readdir THISDIR ;
closedir THISDIR ;

foreach my $file (@allfiles)
{
#if (-d $file)
# {
# print $file."\n";
# };
push @files, $file if (-d $file) ;
}

return (@files)
}

When I run this on the same path as the script is located ('.') it
gives me the correct results.
If I give a path along of where to check, it gives me a few of the
subdirs (for example on the C: drive it would give me the TEMP, the
WINDOWS and the RECYCLER directory, but not the PROGRAM FILES or some
other dirs I have.

I am using Perl 5.8 on a Windows XP machine.

Can anyone shed some light on this strange problem?

Thanx,

Bob
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      11-12-2003
Bob Gervais <(E-Mail Removed)> wrote:
> opendir THISDIR, $dir or die "Error, cannot do opendir on $dir $!";
> my @allfiles = readdir THISDIR ;


[ snip ]

> When I run this on the same path as the script is located ('.') it
> gives me the correct results.


The problem is that readdir() returns filenames, not full paths.

my @subdirs = grep -d, map "$dir/$_", readdir(THISDIR);

[ This group is defunct; please use clp.misc instead. ]

--
Steve
 
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
opendir and readdir vs glob iteration it_says_BALLS_on_your forehead Perl Misc 9 12-03-2005 08:42 AM
Looking for a class similar to 'opendir' and 'readdir' Ramon Java 5 01-03-2005 01:59 PM
using "opendir" through a CGI script on Apache Alex Perl 2 02-10-2004 04:35 AM
Simple opendir(), directory does not exist problem theo22 Perl Misc 12 01-21-2004 01:33 PM
opendir() Andrew Perl 2 09-03-2003 02:48 PM



Advertisments