Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Open a folder

by vinoth.ree (Monsignor)
on Jan 09, 2013 at 05:12 UTC ( #1012378=note: print w/replies, xml ) Need Help??


in reply to Re^2: Open a folder
in thread Open a folder

Hi Dr Manhattan

Below code process each files in the /tmp directory. If any directory is inside the /tmp directory will not be processed

Add additional checking on file, like file size and readable permissions to avoid error.

#!/usr/bin/perl use strict; use warnings; my $tmp_dir="/tmp/"; opendir (DIR, $tmp_dir) or die $!; while (my $file_name = readdir(DIR)) { my $abs_path = $tmp_dir.$file_name; unless(-d $abs_path) #Ignore if it is a directory { &Process_EachFile($abs_path); # Do call a function and process + each file } else { print "$abs_path is a directory\n"; } } closedir(DIR); sub Process_EachFile { my ($filename) = @_; print $filename; open (FH,'<',"$filename") or die $!; while(my $each_line=<FH>) { print $each_line."\n"; #Read file content here and do your calculation. } }

Replies are listed 'Best First'.
Re^4: Open a folder
by Dr Manhattan (Beadle) on Jan 09, 2013 at 12:04 UTC

    Hi ree

    I think I understand what you are trying to do, however when I run your script it crashes- "No such file or directory"

    It has something to do with the

    open (FH,'<',"$filename") or die $!;

    line

    Any advice?

      Hi Dr Manhattan

      when I run your script it crashes- "No such file or directory"

      Could you please confirm that $filename variable contains the absolute path and not a relative path?

      If no problem means you can put code here, I can debug for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012378]
help
Chatterbox?
[marioroy]: The nature of the Guitar
[Discipulus]: my best with guitar is to fingerpicking some De André songs
[karlgoethebier]: Listen to Glenn: https://www. youtube.com/watch? v=SiiVK3rMxxg
[karlgoethebier]: marioroy: If you provide an email address i'll send you the full script of TNOG...
[karlgoethebier]: marioroy: Checkout also "Linear Expressions" by the same author...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-03-24 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (298 votes). Check out past polls.