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

antidote1316 has asked for the wisdom of the Perl Monks concerning the following question:

The problem is that its not reading from the directory...its saying that permission is denied...can someone tell me if there is something wrong with the format...my regex is correct..
#!/usr/bin/perl opendir (DIR, "C:/Perl/bin/Anti") or die "Couldnt open directory: $!"; @files = readdir DIR; closedir (DIR); foreach $file (@files) { open(MYFILE,"C:/Perl/bin/Anti/$file") or die "Count open file :$!"; while(defined($ips=<MYFILE>)){ @array = $ips =~ m/\d+/; for($index=0; $index<@array; $index++) { for($count=0; ;$count++) { print "IP $count is $array[$index] \n"; last; } } } }

Replies are listed 'Best First'.
Re: Unable to read files from a directory
by graff (Chancellor) on May 14, 2009 at 03:43 UTC
    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?

      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).

      This will only work when the current directory happens to be the same as $path. readdir returns bare file names, so in the general case a file should be turned into a full path before sending it to -f. I've been bitten by this more times than I can count (e.g. it works "fine" during testing, but then fails under normal use).

        keeps biting still...nothing seemed to work on Windows...ported it to Linux...works like a charm...what the heck??!!!
      done...again open fails! see this
      #!/usr/bin/perl @files = <C:/Perl/bin/Anti/*>; foreach $file (@files){ print "$file \n"; # found to be ok....prints all the files... open(MYFILE,"<","C:/Perl/bin/Anti/$file") or die "Failed to open file: + $file:$! "; while(<MYFILE>) { print "$_ \n \n \n"; } }
      it prints all the file names... I do not have any sub directories...but I used grep also... fails on file 1. gosh! hey...sorry for the earlier mistakes...I was lost in some world...the count is for some other purpose...was using a stupid method to see the counts of loops... and when I said opened, I meant all permissions... I am able to extract data from the individual files but when I run a loop for the folder, the files are not being opened...its acting weird! thanks for the assistance guys! much appreciated!
        Regarding this latest version of your code, this attempt is failing because now you are populating "@files" with full path strings (because you are using th file glob operator, instead of readdir), and then you are appending the path again inside the foreach loop.

        Using the file glob is a good idea, so now just fix the open statement:

        #!/usr/bin/perl use strict; my @files = <C:/Perl/bin/Anti/*>; for my $file (@files) { my count = 0; open(MYFILE,"<",$file) or do { warn "Failed to open file: $file:$! +\n"; next}; while(<MYFILE>) { $count++; } print "$file has $count lines\n"; }
        BTW, I like proper indenting... don't you?
Re: Unable to read files from a directory
by kennethk (Abbot) on May 14, 2009 at 03:01 UTC
    Have you checked what file this fails on? Your readdir will return the entire contents of the directory, including '.' and '..', which will cause your open to fail. Try modifying

    open(MYFILE,"C:/Perl/bin/Anti/$file") or die "Count open file :$!";

    to

    open(MYFILE,"C:/Perl/bin/Anti/$file") or die "Count open file $file:$!";

    and that will uncover your culprit.

    In a side note, you have a serious potential security issue by using the two-argument form of open in this context. If there is a maliciously named file in your directory of interest, this script can be made to run arbitrary code.

      I'd go somewhat further than your last paragraph and say:

      Always use the three parameter version of open and use lexical file handles:

      open my $inFile, '<', $filename or die "Unable to open $filename: $!";

      True laziness is hard work
      done ...did that...permission denied at untitled 5 line 7.....I dont understand..I opened all the permissions
Re: Unable to read files from a directory
by lakshmananindia (Chaplain) on May 14, 2009 at 02:08 UTC

    Your code is working fine for me

    Check your directory permission and the files inside it

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Re: Unable to read files from a directory
by Marshall (Canon) on May 14, 2009 at 03:29 UTC
    The readdir part looks good to me also.

    I would add "use warnings", you can do this even on windows by #!/usr/bin/perl -w, or you can add a "use warnings;" statement.

    Then I would get to basics to make sure that the permission denied is where you think that it is:

    #!/usr/bin/perl -w opendir (DIR, "C:/Perl/bin/Anti") or die "Couldnt open directory: $!"; @files = readdir DIR; closedir (DIR);
    The problem might be in opening the file for read, not in the directory read itself. See if that works, then
    foreach $file (@files) { print "$file\n"; #just to make sure this looks right }
    I would point out in the above that a Directory is a File! If there is a subdir underneath this DIR, then you will get those also! So you may need something like this:
    @files = grep {"$path/$_" -D }readdir DIR;
    update: it might be -D doesn't mean "not a directory" and that you need "not" the little d. Sorry I've forgotten right now.ok, -f is a lot easier!