http://qs321.pair.com?node_id=763987


in reply to Unable to read files from a directory

First, add use strict;

Next, change the "readdir" line to be like this:

@files = grep { -f } readdir DIR;
That will assure that the @files array contains only things that can properly be opened as files (i.e. things that are not other directories).

Then, as mentioned above, make sure your "die" messages include the actual variable that contains the thing you were trying to open (directory name at line 2, file name at line 7). When you get a "permission denied" error on trying to open a file, check whether some other windows app happens to have that file open. (I gather that on Windows, when some process has a file opened for read/write access, other processes are not allowed to access that file.)

Once you know the specific name of the file that cannot be opened, you could check by other means to see whether some other program might have the same problem with that particular file.

Just a thought: since you are (presumably) looping over a number of files and just reporting some sort of result for each one, why not handle the open failures like this:

my $path = "C:/Perl/bin/Anti"; opendir( DIR, $path ) or die "opendir failed on $path: $!\n"; my @files = grep {-f "$path/$file"} readdir DIR; ### updated as per b +luto's comment below closedir DIR; for my $file ( @files ) { open( MYFILE, "<", "$path/$file" ) or do { warn "open failed on $path/$file: $! -- moving on...\n"; next; }; while (<MYFILE>) { ... } }
Finally, I'm puzzled by the apparent logic of the inner-most for loop. Looks like it shouldn't really be a loop, but in any case, it doesn't make sense.

I don't understand what you meant in your later reply when you said "I opened all the permissions". What does that mean, exactly?