We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,716 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to write out a text file with a date in the file name

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');
    }
2
Contributors
3
Replies
5 Days
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
Xokzin
Newbie Poster
10 posts since May 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.";
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

Thanks, it worked.

Xokzin
Newbie Poster
10 posts since May 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks, it worked.

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

d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1
Question Answered as of 1 Year Ago by d5e5

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5315 seconds using 2.65MB