Download file with current date in filename

A

aloke.sarnobat

Hello,

I am new to this forum, so your help would be most appreciated.

I am trying to create a javascript where, on clicking a button, I will
be able to download the latest log file. But the log filename changes
everyday as it takes the current date as part of the filename. eg.
EMEA_20070806 i.e. EMEA_yyyymmdd
Also, the previous day's log file is stored in the same directory.

How can I create a button where it will download the latest log file
at all times?

Thanks
 
T

Thomas 'PointedEars' Lahn

I am new to this forum, so your help would be most appreciated.

This is a newsgroup, not a (Web) forum. You are accessing Usenet through
the Google archives.
I am trying to create a javascript where, on clicking a button, I will
be able to download the latest log file. But the log filename changes
everyday as it takes the current date as part of the filename. eg.
EMEA_20070806 i.e. EMEA_yyyymmdd
OK.

Also, the previous day's log file is stored in the same directory.

In what way is this relevant to your problem?
How can I create a button where it will download the latest log file
at all times?

It is not a trivial problem because the server time is relevant here. You
would have to retrieve that, e.g. with server-side PHP.

function getLatestLog()
{
var d = new Date(<?php echo time(); ?> * 1000);
var m = d.getMonth() + 1;
var day = d.getDate();
window.location = "http://base-uri.example/EMEA_"
+ d.getFullYear() + (m < 10 ? "0" + m : m)
+ (day < 10 ? "0" + day : day);
}

<script type="text/javascript">
document.write('<input type="button" value="Get Latest Log"'
+ ' onclick="getLatestLog()">');
</script>

However, since you will need server-side scripting anyway, you should do it
all server-side (a possibility: server-side JavaScript in NES-compatibles or
JScript in ASP on IIS). It is much more reliable.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Mon,
6 Aug 2007 21:56:27 said:
function getLatestLog()
{
var d = new Date(<?php echo time(); ?> * 1000);
var m = d.getMonth() + 1;
var day = d.getDate();
window.location = "http://base-uri.example/EMEA_"
+ d.getFullYear() + (m < 10 ? "0" + m : m)
+ (day < 10 ? "0" + day : day);
}


window.location = "http://base-uri.example/EMEA_" +
( 1e4*d.getFullYear() + 1e2*(d.getMonth()+1) + d.getDate() )

should work for AD 1000-9999 inclusive.
 
A

aloke.sarnobat

I created the following, but it doesnt seem to work:

<html>

<head>

<script LANGUAGE="javascript" type="text/javascript">


function getLatestLog()

{

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth()
var year = currentDate.getFullYear()

if (month < 10)

{
month = "0" + month
}

if (day < 10)

{
day = "0" + day
}

var path = "\\sldnaldonstp02\onsitestore\OnSiteData\MOM\LOGS\MOM_EES."
+ year + '-' + month + "-" + day + ".LOG"
//var path =
"\\sldnaldonstp02\onsitestore\OnSiteData\MOM\LOGS\MOM_EES."
+ year + '-' + month + "-" + day + ".LOG"

document.open(path) ;
}

</script>

</head>

<body>

<form name="myform">

<input type="button" value="MOM LOGS" onclick="getLatestLog()">

</form>

</body>

</html>
 
L

Lee

(e-mail address removed) said:
I created the following, but it doesnt seem to work:

<html>

<head>

<script LANGUAGE="javascript" type="text/javascript">


function getLatestLog()

{

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth()
var year = currentDate.getFullYear()

if (month < 10)

{
month = "0" + month
}

if (day < 10)

{
day = "0" + day
}

var path = "\\sldnaldonstp02\onsitestore\OnSiteData\MOM\LOGS\MOM_EES."
+ year + '-' + month + "-" + day + ".LOG"
//var path =
"\\sldnaldonstp02\onsitestore\OnSiteData\MOM\LOGS\MOM_EES."
+ year + '-' + month + "-" + day + ".LOG"

document.open(path) ;
}

</script>

For debugging purposes, change:
document.open(path);
to:
alert(path);

and you should see what you forgot to do while processing the date.


--
 
T

Thomas 'PointedEars' Lahn

I created the following, but it doesnt seem to work:

"Does not work" is a useless error description. [psf 4.11]
<html>
[...]
http://validator.w3.org/

[...]
function getLatestLog()

{

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth()

You forgot to add the 1. The retrieved month number is zero-based (0 for
January, 1 for February aso.)
var year = currentDate.getFullYear()

if (month < 10)

{
month = "0" + month
}

if (day < 10)

{
day = "0" + day
}

I'd go with John's approach instead.
var path = "\\sldnaldonstp02\onsitestore\OnSiteData\MOM\LOGS\MOM_EES."
+ year + '-' + month + "-" + day + ".LOG"

1. The backslash starts an escape sequence in string literals,
you have to escape it (double it) if you need it literally.

2. This is not a URI, it is a UNC path. Internet != Windows.
The corresponding URI would be

file://///sldnaldonstp02/onsitestore/OnSiteData/MOM/LOGS/MOM_EES...

but probably you are looking for something that starts with "http:".
[...]
document.open(path) ;

Could you please abandon your trial-and-error approach of doing things and
read the docs for a change? document.open() does not display another
document, it opens the current document for (temporary) writing.

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-72161170

You were looking for

window.location = path;

However, that would work only with an URI.


HTH

PointedEars
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top