Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Open a directory and recursively process files

by Dr Manhattan (Beadle)
on Jan 09, 2013 at 20:06 UTC ( [id://1012560]=note: print w/replies, xml ) Need Help??


in reply to Re: Open a directory and recursively process files
in thread Open a directory and recursively process files

Hi blue cowdawg

I tried your sub, but it still does not work. ".:Permission denied at project6.pl line 16." Which is    open FIN,"< $fname" or die "$fname:$!"; Any idea why this happens?

#!/usr/bin/perl -w use strict; my $dir = 'C:\Users\Zandre Botha\Desktop\Text Files'; opendir (DIR, $dir) or die "cannot opendir $dir"; foreach my $file (readdir(DIR)) { &process_file ($file); } sub process_file { my $fname=join("/",@_); open FIN,"< $fname" or die "$fname:$!"; my @lines=<FIN>; chomp @lines; close FIN; my $count=0; foreach my $line(@lines) { map { $count++ } split (/[\s\t\n]+/,$line); } printf "There are %d words in %s\n",$count,$fname; } closedir (DIR);

Replies are listed 'Best First'.
Re^3: Open a directory and recursively process files
by blue_cowdawg (Monsignor) on Jan 09, 2013 at 20:31 UTC
        I tried your sub, but it still does not work. ".:Permission denied at project6.pl line 16." Which is open FIN,"< $fname" or die "$fname:$!"; Any idea why this happens?

    If you read the diagnostic carefully you'll notice it complains about the filename "." which is actually your current working directory. You forgot the line

    next if ($file eq '.') || ( $file eq '..');
    just prior to the call to the sub. I think I mentioned that a few answers ago...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re^3: Open a directory and recursively process files
by Anonymous Monk on Jan 09, 2013 at 20:12 UTC

    Any idea why this happens?

    Because you don't have permission -- ask the computer why

    stat, file, ls

    or die Fudge( "q{$file}:" ); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; }
Re^3: Open a directory and recursively process files
by marto (Cardinal) on Jan 09, 2013 at 23:30 UTC
    You failed to include a couple of lines of code you were given the last time you asked this question. Guess which ones:Re^2: Open a folder

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1012560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found