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

print out certain extension files

by ytjPerl (Scribe)
on Sep 08, 2017 at 18:26 UTC ( [id://1198971]=perlquestion: print w/replies, xml ) Need Help??

ytjPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a script to print out all the files with 'txt' extension, but it prints out multiple times instead of once. but I do not intend to do it in my script.
use strict; my $DIR = "C:/Users/tyang/Documents/Traning"; opendir DIR, $DIR or die "could not open directory: $!"; while (my $file = readdir DIR) { my @files = grep ( -f ,<*.txt>); # Write them out foreach (@files) { print $file, ' '; my $age = -M $file; if ($age > 7) {print "the age is", int($age),"old than a week\n";} } }

Replies are listed 'Best First'.
Re: print out certain extension files
by haukex (Archbishop) on Sep 08, 2017 at 18:36 UTC

    <*.txt> is the equivalent of glob('*.txt'), which is a function that returns a list of files. In addition, you are iterating over the list of files in DIR with your readdir loop. You should only use one of these two methods. glob is a bit easier to use than readdir, although you should use File::Glob ':bsd_glob'; to prevent issues with whitespace in the argument, and also keep in mind that glob does not list filenames beginning with a dot by default.

    use warnings; use strict; use File::Glob ':bsd_glob'; my $DIR = "C:/Users/tyang/Documents/Traning"; my @files = glob("$DIR/*.txt"); for my $file (@files) { next unless -f $file; print $file, ' '; my $age = -M $file; if ($age > 7) { print "the age is ",int($age)," old than a week"; } print "\n"; }

    Update: Added the -f check.

      super thanks. Can I know what the function of -f check? I could not find it when googling.
        You can read about it here -X.
Re: print out certain extension files
by Laurent_R (Canon) on Sep 08, 2017 at 18:47 UTC
    You basically have two nested loops on files: one with the while (my $file = readdir DIR), which reads each file entry in turn, and, then, again with the foreach (@files). Keep only one of them.
Re: print out certain extension files
by thanos1983 (Parson) on Sep 09, 2017 at 14:15 UTC

    Hello ytjPerl,

    It looks fellow Monks have answered your question. I can not resist to post another suggestion with one of my favourite modules File::Find::Rule. Sample of code and implementation on one of the multiple questions on the forum Pick up specific file name. But it has so many more possibilities than what I just mentioned.

    Why I propose this module? Well my main reasons, is that you can use multiple regex to get any kind of file(s) you want, you can search on multiple directories recursively and also that you can define the depth of search.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1198971]
Approved by haukex
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 22:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found