I'm reading a file and trying to write it out with the current date in the file name.
But I'm getting an error when trying to run this code

use D_Db;

    my $dbh = D_Db::connect('EDW');
    my $Curr_dt=D_Dates::get_curr_ccyy_mm_dd({dbh=>$dbh});
        my $sth4BobExt=$dbh->prepare("Select * from TableX");
    while ( my @BobExtrow = $sth4BobExt -> fetchrow_array())
      #print @BobExtrow;
      my $Bob_Ext=$BobExtrow[0];
      #print "\n$Bob_Ext\n";
      my $d=`date +%F|awk -F '-' '{print \$1 \$2 \$3}'`;
      D_OS::run_cmd('echo "@BobExtrow" >> /data/hosstg/TableX_T-$d.txt');

Recommended Answers

All 3 Replies

I don't have your kind of database and am not familiar with awk so I don't understand your script. To create an output file whose name is the current date you can do the following:

use strict;
use warnings;

my ($day, $month, $year) = (localtime)[3,4,5];
my $filename = sprintf("%04d-%02d-%02d\n", $year+1900, $month+1, $day);

open my $fh, '>', $filename;

print $fh "This file should have today's date as it's name.";

Thanks, it worked.

Thanks, it worked.

You're welcome. Please don't forget to mark this thread 'solved'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.