Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 $!;


    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?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2015-11-25 21:25 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (691 votes), past polls