Download file with current date in filename

Discussion in 'Javascript' started by aloke.sarnobat@gmail.com, Aug 6, 2007.

  1. Guest

    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
    , Aug 6, 2007
    #1
    1. Advertising

  2. wrote:
    > 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
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Aug 6, 2007
    #2
    1. Advertising

  3. In comp.lang.javascript message <>, Mon,
    6 Aug 2007 21:56:27, Thomas 'PointedEars' Lahn <>
    posted:
    >
    > 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.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr J R Stockton, Aug 7, 2007
    #3
  4. Guest

    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>
    , Aug 8, 2007
    #4
  5. Lee Guest

    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.


    --
    Lee, Aug 8, 2007
    #5
  6. wrote:
    > 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
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Aug 8, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,006
    Travis Murray
    Aug 24, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  3. Replies:
    1
    Views:
    1,437
    Roland de Ruiter
    Jun 15, 2006
  4. Ed
    Replies:
    10
    Views:
    45,751
    alok000707
    Jul 13, 2010
  5. Stefano Crocco
    Replies:
    0
    Views:
    134
    Stefano Crocco
    Nov 14, 2008
Loading...

Share This Page