Hi,

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");
    $sth4BobExt->execute();
    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}'`;
      chomp($d);
      D_OS::run_cmd('echo "@BobExtrow" >> /data/hosstg/TableX_T-$d.txt');
    }

Dani AI

Generated

A few best-practice notes that build on 's original attempt and 's localtime idea: compute a single date string outside the loop, open the output file once with a proper Perl filehandle, and print rows directly rather than shelling out with echo. That avoids repeated process creation, prevents shell‑injection risks, and keeps filenames free of stray newlines or whitespace.

use strict;
use warnings;
use POSIX qw(strftime);

my $date = strftime('%Y%m%d', localtime);        # YYYYMMDD (no newline)
my $outpath = "/data/hosstg/TableX_T-$date.txt";

open my $out, '>', $outpath or die "Cannot open $outpath: $!";
binmode $out, ':encoding(UTF-8)';

while ( my $rowref = $sth4BobExt->fetchrow_arrayref ) {
    chomp for @$rowref;
    print $out join("\t", @$rowref), "\n";
}

close $out or warn "close failed: $!";

Practical cautions and tips:

  • Compute the date once (outside the loop) so the filename is stable and you don’t re-run external commands repeatedly.
  • Use fetchrow_arrayref to avoid copying arrays every iteration; join fields to a safe delimiter (use Text::CSV for true CSV output).
  • Always use 3-arg open and check errors (or die / warn) so you get a clear reason if the file can’t be created (permissions, missing directory, etc.).
  • If multiple processes might append to the same file, use flock to avoid interleaved writes.
  • Avoid backticks or shelling out to build filenames — they can add newlines or produce locale-dependent results. If an external date program is required, use system with a LIST form or capture and sanitize its output.

These changes make the script faster, safer, and easier to debug while preserving the simple goal: write the DB rows to a file whose name contains the current date.

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:

#!/usr/bin/perl
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, networking, learning, and sharing knowledge.