Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?


In reply to Re: Unable to read files from a directory by graff
in thread Unable to read files from a directory by antidote1316

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found