I have a list of .DTA files with below dates.

Jun 26 02:53 fs261803.DTA
Jun 26 02:54 fs261804.DTA
Jun 26 02:56 fs261805.DTA
Jun 26 03:25 fs261865.DTA
Jun 26 03:27 fs261869.DTA
Jun 27 03:21 fs271865.DTA
Jun 27 03:23 fs271869.DTA
Jun 28 03:23 fs281865.DTA
Jun 28 03:25 fs281869.DTA
Jun 29 03:21 fs291865.DTA
Jun 29 03:23 fs291869.DTA
Jun 29 03:54 fs291803.DTA

I want to write all jun 29 files data's into one file. I have the logic to combine multiple files into one. Below is my code.


use strict;
use warnings;
use feature 'say';

my $start_pos = 0;
my $data_len  = 15;
my $pad_len   = 54;

my $dir = ".";
opendir(DIR, $dir) or die "Cannot open directory: $dir!\n";
my @files = readdir(DIR);
open my $out, ">>output.trns" or die "Cannot open output.trns!\n";

foreach my $file (@files)
    if($file =~ /DTA$/)
        #if ($file=`ls -rlt *.DTA` )
        #print "$file\n";
        open my $in, "<$file" or die "Cannot open $file!\n";
        while (<$in>) {
        say $out ' ' x $pad_len, substr($_, $start_pos, $data_len);

        close $in;
close $out;

In this code I can combine all the .DTA files into one. But I need to combine only Jun 29 files to create one new file. Can anyone help me out in perl?


In order to compare the file's timestamps (presumably the modification time), you need to stat() each file and compare the corresponding year, month, and day. Here's an example that hard-codes a filter for 2018-06-29:

next unless $file =~ /DTA$/;
my $fullpath = $dir . "/" . $file;
my $wantedyear=2018;
my $wantedmonth=5; ## zero-based
my $wantedday=29;
my $mtime=(stat $fullpath)[9];
my $fileyear=(localtime($mtime))[5] + 1900;
next unless $fileyear == $wantedyear;
my $filemonth=(localtime($mtime))[4];
next unless $filemonth == $wantedmonth;
my $fileday=(localtime($mtime))[3];
next unless $fileday == $wantedday;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.