CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
loss of readdir function with vista

 



DrRob
Novice

Apr 6, 2009, 3:58 PM

Post #1 of 19 (4065 views)
loss of readdir function with vista Can't Post

I havebeen using readdir to identify files in directories for some time now. Last week routines which had worked fine before stopped giving the list of files I was expecting; the following is a routine which until recently worked fine for reading files in folders and sub-folders recurisvely. Any ideas (apart from it's just crappy Vista)?

sub read_folder {

foreach my $folder (@_) {

opendir DIR, $folder || die "Can't open dir $folder";
my @files = grep {-f "$folder$_"} readdir(DIR) || print "Failed to read $folder\n"; # select out ordinary files
rewinddir DIR || print "Failed to read $folder\n";
my @folders = grep {-d "$folder$_" && !/(\.|\.\.)/} readdir(DIR)|| print "Failed to read $folder\n"; # select out folders
closedir DIR || print "Failed to read $folder\n";

$folder_files{$folder} = [ @files ];

for (my $i=0;$i<=$#folders;$i++) {$folders[$i] = $folder.$folders[$i]."/";}

if (@folders) {read_folder(@folders);}
}

return;
}


KevinR
Veteran


Apr 6, 2009, 5:03 PM

Post #2 of 19 (4064 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

try adding in some print lines so you can watch the script run and see if you spot anything.
-------------------------------------------------


FishMonger
Veteran / Moderator

Apr 6, 2009, 9:19 PM

Post #3 of 19 (4061 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

File::Find - Traverse a directory tree.
http://search.cpan.org/~nwclark/perl-5.8.9/lib/File/Find.pm


DrRob
Novice

Apr 17, 2009, 12:57 AM

Post #4 of 19 (4003 views)
Re: [FishMonger] loss of readdir function with vista [In reply to] Can't Post

Have cut out any clever stuff. readdir simply returns . (i.e. fullstop there are always . and .. entries in any folder) for any folder read.

Is this Vista ?security change or is it my computer ?corrupt Perl library. MIght try re-installling Perl.

I don't suppose anyone out there is using Vista


FishMonger
Veteran / Moderator

Apr 17, 2009, 5:15 AM

Post #5 of 19 (3998 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

I tried Vista a couple times and hated it. I won't switch over to it until I have no other options.

The problem you're having may be Vista security settings, or your script, but I doubt that it's with Perl. However, removing and reinstalling the latest version of Perl might be a good start.

Can you show us the complete script? If not, then please post the latest version of the read_folder sub.

Are you running under strictures and warnings? If not, then add the warnings and strict pragmas and post the warnings and errors that the script generates.


KevinR
Veteran


Apr 17, 2009, 10:37 AM

Post #6 of 19 (3995 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

Are you sure this is correct:


Code
sub read_folder {  
foreach my $folder (@_) {


what is the value of $folder at that point in the script? Are they just folder/file names or is there a path inculded?
-------------------------------------------------


KevinR
Veteran


Apr 17, 2009, 10:44 AM

Post #7 of 19 (3994 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

See if this works any better:


Code
sub read_folder {  
my(@list) = @_;
foreach my $folder (@list) {
opendir (DIR, $folder) or die "Can't open dir $folder :$!";
closedir (DIR) or die "Failed to close DIR: $!\n";
my @dir = readdir(DIR);
my $files = grep {-f "$folder/$_" && !/^\.+$/} @dir;
my @folders = grep {-d "$folder/$_" && !/^\.+$/} @dir;
print "Files:\n";
print "$_\n" for @files;
print "\nFolders\n";
print "$_\n" for @folders;
}
}

-------------------------------------------------


(This post was edited by KevinR on Apr 17, 2009, 10:45 AM)


DrRob
Novice

Apr 18, 2009, 4:27 AM

Post #8 of 19 (3987 views)
Re: [KevinR] loss of readdir function with vista [In reply to] Can't Post

Thanks Kevin but yours doesn't work either. And yes $folder is technically a path - maybe I should rename my variable. $folder contains a valid path and strict and warning are in use and the script does not generate any errors - just no useful data.

Will try re-installin Perl as this is just too irritating.

Cheers R


DrRob
Novice

Apr 18, 2009, 4:51 AM

Post #9 of 19 (3985 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

Have re-installed ActivePel 5.10

and readdir still doesn't work.

Might try Activestate

Bizarre


FishMonger
Veteran / Moderator

Apr 18, 2009, 5:24 AM

Post #10 of 19 (3984 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post


In Reply To
Thanks Kevin but yours doesn't work either. And yes $folder is technically a path - maybe I should rename my variable. $folder contains a valid path and strict and warning are in use and the script does not generate any errors - just no useful data.


If you didn't receive an error from Kevin's code, then obviously you didn't have strict or warnings enabled.


Code
C:\testing>type kevin.pl 
#!/usr/bin/perl

use strict;
use warnings;

read_folder('c:/testing');

sub read_folder {
my(@list) = @_;
foreach my $folder (@list) {
opendir (DIR, $folder) or die "Can't open dir $folder :$!";
closedir (DIR) or die "Failed to close DIR: $!\n";
my @dir = readdir(DIR);
my $files = grep {-f "$folder/$_" && !/^\.+$/} @dir;
my @folders = grep {-d "$folder/$_" && !/^\.+$/} @dir;
print "Files:\n";
print "$_\n" for @files;
print "\nFolders\n";
print "$_\n" for @folders;
}
}

C:\testing>kevin.pl
Global symbol "@files" requires explicit package name at C:\testing\kevin.pl line 17.
Execution of C:\testing\kevin.pl aborted due to compilation errors.

C:\testing>type kevin.pl
#!/usr/bin/perl

use strict;
use warnings;

read_folder('c:/testing');

sub read_folder {
my(@list) = @_;
foreach my $folder (@list) {
opendir (DIR, $folder) or die "Can't open dir $folder :$!";
closedir (DIR) or die "Failed to close DIR: $!\n";
my @dir = readdir(DIR);
my @files = grep {-f "$folder/$_" && !/^\.+$/} @dir;
my @folders = grep {-d "$folder/$_" && !/^\.+$/} @dir;
print "Files:\n";
print "$_\n" for @files;
print "\nFolders\n";
print "$_\n" for @folders;
}
}

C:\testing>kevin.pl
readdir() attempted on invalid dirhandle DIR at C:\testing\kevin.pl line 13.
Files:

Folders

C:\testing>


Now, lets move that close statement down a little.

Code
C:\testing>type kevin.pl 
#!/usr/bin/perl

use strict;
use warnings;

read_folder('c:/testing');

sub read_folder {
my(@list) = @_;
foreach my $folder (@list) {
opendir (DIR, $folder) or die "Can't open dir $folder :$!";
my @dir = readdir(DIR);
my @files = grep {-f "$folder/$_" && !/^\.+$/} @dir;
my @folders = grep {-d "$folder/$_" && !/^\.+$/} @dir;
closedir (DIR) or die "Failed to close DIR: $!\n";
print "Files:\n";
print "$_\n" for @files;
print "\nFolders\n";
print "$_\n" for @folders;
}
}

C:\testing>kevin.pl
Files:
10.100.0.1-150
10.100.0.106
A0001002-1.txt
abstract.pl
apc.pm
auto_it.txt
bazz.pl
BetfairAPI6Examples.pm
bladez1.pl
bladez2.pl
company.csv
Constants.pm

<snipe long list>



FishMonger
Veteran / Moderator

Apr 18, 2009, 5:27 AM

Post #11 of 19 (3983 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

It's difficult to help you troubleshoot if you don't show us exactly what you tested and you also need to tell us the truth about the results of a test.


FishMonger
Veteran / Moderator

Apr 18, 2009, 5:39 AM

Post #12 of 19 (3980 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

This is the method I'd use.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use File::Find;

my (@files, @folders);

find({ wanted => \&read_folder, no_chdir => 1 }, 'c:/testing');

sub read_folder {
return if $_ =~ /^\./;
push @files, $_ and return if -f $_;
push @folders, $_ and return if -d $_;
}

print "Files:\n";
print "$_\n" for @files;
print "\nFolders\n";
print "$_\n" for @folders;



(This post was edited by FishMonger on Apr 18, 2009, 5:41 AM)


DrRob
Novice

Apr 18, 2009, 5:46 AM

Post #13 of 19 (3978 views)
Re: [FishMonger] loss of readdir function with vista [In reply to] Can't Post

I did get the error message

When I read it originally I thought that might not work

I simply edited and didn't mention the error because I though that would be rude....

here is an example;

#Test readdir

use strict;
use warnings;

my $root = "D:/SD/";

chdir $root;

opendir DIR, "$root" || die "Can't open dir $!";
my @entries = readdir(DIR) || print "Failed to read $!\n";
closedir DIR || print "Failed to read $!\n";
print $root,"\n",@entries,"\n\n";

No errors, just

D:/SD/
.


I am just in the process of updating to build 1004 of 5.10


FishMonger
Veteran / Moderator

Apr 18, 2009, 6:26 AM

Post #14 of 19 (3972 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $root = "D:/SD/";
my @entries;

chdir $root or die "failed to cd to $root $!";

opendir(DIR, $root) || die "Can't open $root dir $!";
while( my $file = readdir(DIR) ) {
push @entries, $file;
}
closedir DIR || warn "Failed to close $root dir $!\n";
print "$root\n";
print "$_\n" for @entries;



FishMonger
Veteran / Moderator

Apr 18, 2009, 6:50 AM

Post #15 of 19 (3970 views)
Re: [DrRob] loss of readdir function with vista [In reply to] Can't Post

If you don't want to use the while loop, then simply remove || print "Failed to read $!\n" from the readdir line.


DrRob
Novice

Apr 18, 2009, 8:15 AM

Post #16 of 19 (3967 views)
Re: [FishMonger] loss of readdir function with vista [In reply to] Can't Post

Brilliant

genius

it was the || print "Can't read etc that stopped it doing a loop

opendir DIR, "$root" || die "Can't open dir $!";
my @entries = readdir(DIR);
closedir DIR || print "Failed to read $!\n";
print $root,"\n",@entries,"\n\n";

works fine

At some point I must have added that bit to the routine, thinking it would make it more robust. Then forgot about it and went back to it later only to find it wasn't reading a juts the first entry.

Thanks to everyone for their effort and help.

Rob J


KevinR
Veteran


Apr 18, 2009, 12:40 PM

Post #17 of 19 (3963 views)
Re: [FishMonger] loss of readdir function with vista [In reply to] Can't Post

Good catches Fish, thats what I get for trying to help when I'm doped up on Vicodin. Crazy

I appreciate your spotting the problems and correcting them and helping DrRob. Cool
-------------------------------------------------


FishMonger
Veteran / Moderator

Apr 18, 2009, 12:51 PM

Post #18 of 19 (3961 views)
Re: [KevinR] loss of readdir function with vista [In reply to] Can't Post


In Reply To
Good catches Fish, thats what I get for trying to help when I'm doped up on Vicodin. Crazy


Now what did you do? Too much rough sex? Cool

If you run low, I think I have a bottle left over from my heart surgery. Like on old jalopy, I needed a valve job. Frown


KevinR
Veteran


Apr 18, 2009, 12:58 PM

Post #19 of 19 (3958 views)
Re: [FishMonger] loss of readdir function with vista [In reply to] Can't Post


In Reply To

In Reply To
Good catches Fish, thats what I get for trying to help when I'm doped up on Vicodin. Crazy


Now what did you do? Too much rough sex? Cool

If you run low, I think I have a bottle left over from my heart surgery. Like on old jalopy, I needed a valve job. Frown


Don't I wish it was too much rough sex! Cool

I'm a bit of an old jalopy too, arthritist in the back, vicodin is the only thing that really helps. Unsure
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives