Here I added the code for read the .txt format files only. And the remaining things are same.
use strict;
use warnings;
my %hash;
# Declare your directory
my $dir='e:/dani/csv/fruit';
# read the txt file in the $dir
opendir(DIR, $dir) || die "Cannot open the $dir : $!";
my @fruit = grep /\.txt$/, readdir(DIR);
closedir(DIR);
# get the latest file for each Scenario
for (@fruit) {
chomp;
my ($fruit,$end)=split(/\_/);
$hash{$fruit}=0 if(!$hash{$fruit});
my $stamp = $1 if ($end=~ m{(\d+)});
if($stamp>$hash{$fruit}){
$hash{$fruit}=$stamp;
}
}
for (keys %hash){
print "Last $_ = $_"."_"."$hash{$_}\.txt\n";
}
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
my @fruit = grep /\.txt$/, readdir(DIR);
The above code grep the all .txt format file. But you want extract the time stamp files only. So you modify the below line in the previous code.
# grep the underscore follwed by digit with .txt format file
my @fruit = grep /\_\d+\.txt$/, readdir(DIR);
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24