Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Open a folder

by vinoth.ree (Prior)
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. } }


Comment on Re^3: Open a folder
Download Code
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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2015-07-30 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls