Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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


    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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-06-20 10:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.