Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Open a folder

by Dr Manhattan (Beadle)
on Jan 08, 2013 at 13:26 UTC ( #1012229=note: print w/replies, xml ) Need Help??

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


This is the first progress I've made in some time, so thank you very much.

When I run your code I get a list of all the file names within my directory.

However I want open these text files as well, and then do some basic calculations(I have put these in a sub) in them. How can I iterate through all the text files within the directory and run my sub on each file?

Replies are listed 'Best First'.
Re^3: Open a folder
by marto (Archbishop) on Jan 08, 2013 at 14:28 UTC

    With the example you've been given there's a print statement within a while loop. Replace the print statement with the actions you wish to be taken, checking it's a text file, performing the operations you want., tutorials.

Re^3: Open a folder
by clueless newbie (Chaplain) on Jan 08, 2013 at 16:49 UTC

    Since you mentioned File::Find ... Change the sub _SizeAndMD5 to suit your problem.

    #! use strict; use warnings; use File::Find; use Digest::MD5::File; no warnings "File::Find"; use Smart::Comments; local *wanted=sub { if (-d $File::Find::name) { # directory } elsif ($File::Find::name =~ m{\.txt$}i) { # a file of interest print "for $File::Find::name - @{[_SizeAndMD5($File::Find::nam +e)]}\n"; } }; # wanted # Do something based on the file's content local *_SizeAndMD5=sub { my ($filename_S)=@_; return (-e $filename_S ? sprintf("%8.8lx-%32.32s",-s $filename_S,D +igest::MD5::File::file_md5_hex($filename_S)) : undef); }; # _SizeAndMD5: File::Find::find({ no_chdir=>1,wanted=>\&wanted },@ARGV); exit;

    This will process all of the files with a ".txt" extension below the directories specified by @ARGV.

Re^3: Open a folder
by vinoth.ree (Monsignor) on Jan 09, 2013 at 05:12 UTC
    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. } }

      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://1012229]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-04-26 04:29 GMT
Find Nodes?
    Voting Booth?