qiuckie for creating a file with date in name

L

laredotornado

Hi, This group has been pretty unbelievable at coming up with quick one
or two line solutions to questions so I thought I'd throw this one out
there. I want to create a file containing a single word "Done" for
contents but I would like the file name to be of the form:

File_YYYYMMDD_HHMMSS.done

where YYYY is the four year date, MM is the month number, and so on..
What's the quickest way I could do this?

Thanks a million! - Dave
 
P

Paul Lalli

Hi, This group has been pretty unbelievable at coming up with quick one
or two line solutions to questions so I thought I'd throw this one out
there. I want to create a file containing a single word "Done" for
contents but I would like the file name to be of the form:

File_YYYYMMDD_HHMMSS.done

where YYYY is the four year date, MM is the month number, and so on..
What's the quickest way I could do this?

probably by using localtime()

Something along the lines of
my @time = localtime(time);
my $file = sprintf("File_%4d%2d%2d_%2d%2d%2d.done",
$time[5]+1900,$time[4]+1, @time[3,2,1,0]);

read about localtime in
perldoc -f localtime

Paul Lalli
 
B

Brian McCauley

Paul said:
Something along the lines of
my @time = localtime(time);
my $file = sprintf("File_%4d%2d%2d_%2d%2d%2d.done",
$time[5]+1900,$time[4]+1, @time[3,2,1,0]);

That almost works but is unsightly. The OP probably wanted numbers
padded with leading zeros not spaces.

There is, however, another function you can use in place of sprintf()
especially for this job to be found in one of the standard Perl modules.

This function takes a template a bit like that used in sprintf() but the
elements of the template directly correspond to various representations
of bits of the time structure return by localtime().
read about localtime in
perldoc -f localtime

And while you are there you can also read about the function I mention
above.
 
T

Tintin

Hi, This group has been pretty unbelievable at coming up with quick one
or two line solutions to questions so I thought I'd throw this one out
there. I want to create a file containing a single word "Done" for
contents but I would like the file name to be of the form:

File_YYYYMMDD_HHMMSS.done

where YYYY is the four year date, MM is the month number, and so on..
What's the quickest way I could do this?

use POSIX 'strftime';
my $file = strftime('File_%Y%m%d_%H%M%S.done',localtime);
open FILE, ">$file" or die "Could not create $file $!\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top