Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Open a folder

by vinoth.ree (Parson)
on Jan 08, 2013 at 10:37 UTC ( #1012218=note: print w/ replies, xml ) Need Help??


in reply to Open a folder

Opening the directory

To open the directory,use a function called "opendir". You use this same like the open function to open files. Below example shows opening of /tmp directory.

#!/usr/bin/perl use strict; use warnings; my $tmp_dir="/tmp"; opendir (DIR, $tmp_dir) or die $!;
Reading the directory

To read the files and directories in the directory we use the readdir function. readdir returns the name of each file or directory in the opened directory in turn when used in scalar context, or a list of the names of all files and directories in that directory when used in list context. This means that we can use readdir in a foreach loop or any other loop

while (my $file_name = readdir(DIR)) {print "$file_name\n";}
Closing the directory

We use the function closedir to close the directory once we are finished with it. Like files, the directory will be closed when the program finish, but sometimes need to close the directory opened

closedir(DIR);


Comment on Re: Open a folder
Select or Download Code
Re^2: Open a folder
by Dr Manhattan (Beadle) on Jan 08, 2013 at 13:26 UTC

    Hi

    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?

      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. http://learn.perl.org, tutorials.

      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.

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

        line

        Any advice?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2014-12-29 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls