Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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]
Discipulus is Corion reading 'Gory details of parsing quoted constructs'?..
[perldigious]: That's awesome erix. One of my work friends, who coincidentally convinced me to learn Perl originally, has a Finnish pen-pal. I showed him this and he immediately wanted to send it along to her.
[perldigious]: Also coincidentally, I'm currently in the middle of a winter storm that has dropped 1 foot of snow and is expected to drop another. I might have to adapt this for customers/vendors who come visit and put it at the beggining of every presentation I give.
[Discipulus]: the photo was removed... Cyrano de Berjerac was not of the same idea..

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2016-12-06 16:24 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.