<*.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.
In reply to Re: print out certain extension files
by haukex
in thread print out certain extension files
by ytjPerl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |