Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append Timestamp to Filename 1

Status
Not open for further replies.

lwinstead

IS-IT--Management
Feb 4, 2002
157
US
I'm playing with the formmail script, and I'd like to output the form data to a file, with a timestamp in the filename. This is happening on a Win2k3 Server, running IIS 6.0. I've got the formmail script working in its basic functions, ie I can fill out a form and have it e-mail me.

Does anyone know how to append a timestamp to a filename in Perl? Thanks in advance...

<<<<[flux]>>>>
 
Something like the following:

Code:
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;

my $datetime = sprintf "%02d%02d%04d%02d%02d%02d", $mon, $mday, $year, $hour, $min, $sec;

my $file = param("file_upload") . $datetime;

my ($total_size, $size, $data);
 
open(SAVE, ">$outputdir/$filename") or die "Can't open file: $!";
 
binmode SAVE;
 
while($size = read(param("file_upload"), $data, 1024))
{
    print SAVE $data;
    $total_size += $size;
}
 
close(SAVE);

- George
 
Might be better im a YMD format for searching

Just a thought

--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Whoops! Sorry, force of habit... Paul's right... Should be:

Code:
my $datetime = sprintf "%04d%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec;

My mistake.

- George
 
Rieekan,

Thanks for the code, but I'm a little confused. Is all that just for the sake of appending the datetime to the filename? And what is the filename itself, in your example code? Considering this formmail script is the very first thing I've ever done with Perl, I'm a newbie, but this seems more complex than it should be.

Can you break it down a little?

<<<<[flux]>>>>
 
Well, I'm not sure what the Formmail script you are currently using does, so I just put in some pseudo code to show you how to do it.

Broken down, it does the following:
Code:
# Get the current time from the server
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon += 1;

#create you timestamp format to append
my $datetime = sprintf "%04d%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec;

#set the file name equal to the timestamp and file name given by the HTML form
my $file =  $datetime . '-' . param("file_upload");

#initialize some variables
my ($total_size, $size, $data);
my $outputdir = '/home/user/file/dir';

#open the file you want to save the streaming data to
open(SAVE, ">$outputdir/$filename") or die "Can't open file: $!";

#set the filehandle to binary mode so you can accept all files
binmode SAVE;

#While you get data from the Form
while($size = read(param("file_upload"), $data, 1024))
{
    #print the data chunk to the file handle you created on your server
    print SAVE $data;
    #add in the size you just wrote (used if you want to limit file sizes)
    $total_size += $size;
}

#finished getting the file, so close the handle 
close(SAVE);
 
Rieekan,

Ah, now I see what I previously missed. I'm modifying the formmail 1.92 Blat script from Matt Wright's site. There wasn't any explained way to add the datetime, so I thought I'd try it myself.

Anyway, I hadn't fully understood how you used the new filename, but now I see. Its a bit different than the stock method used in Matt's script, but I can comprehend it just fine, now.

Thanks a ton!

<<<<[flux]>>>>
 
Hi

Am I missing something ? This is abit strange for me :
Rieekan said:
my [red]$file[/red] = $datetime . '-' . param("file_upload");
[gray](...)[/gray]
open(SAVE, ">$outputdir/[red]$filename[/red]") or die "Can't open file: $!";
From where is coming that [tt]$filename[/tt] variable ?

Feherke.
 
No, I was missing something... that wonderful tool of all developers... Caffeine... $filename should be $file...

- George
 
Rieekan,

Thanks a bunch, bub. I basically copied your code into the formmail script, minus some of the file population lines, and it works perfectly. I also immediately understood the mistake with the $filename/$file handle, so I took care of that right away. Thanks again!!

<<<<[flux]>>>>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top