Hitachi Vantara Pentaho Community Forums
Results 1 to 5 of 5

Thread: javascript date difference

  1. #1

    Arrow javascript date difference

    I want to set up an aging process to delete files from a directory after "X" days old.

    So, I've got a Get File Names step that flows into a Modified java Script step. The javascript I'm
    using below but I'm not getting any deletes with it. I've included a graphic of folder and a copy of the
    log file.

    // Determine file date and delete after X days old


    // Figure out today's date.
    var dtNow = new Date();

    try
    {
    // If X days old, delete
    var diff = dtNow - lastmodifiedtime.getDate();
    if (diff >= 2)
    {
    deleteFile(filename.getString());
    }
    }
    catch(err)
    {
    var warn="warning";
    }


    Any ideas?
    David
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2

    Default

    You might have other requirements that you didn't share but I wouldn't use PDI for a job like this. I'd put something like "find . -type f -mtime +30 -exec rm -f {} \;" in a crontab and call it good.

    --Jeff

  3. #3

    Default

    Jeff, thanks for the reply. Unfortunately, I'm using Windows XP. Also, the delete file job is reaching across to the IFS of an AS400 and I'm not an Administrator. So, my choices are limited.

    I did get a little further with the following.

    // Determine file date and delete after X days old

    // Figure out today's date.
    var dtNow = new Date();

    //Alert(dateDiff(dtNow,lastmodifiedtime.getDate(),"d"));


    try
    {

    //Alert(dateDiff(dtNow,lastmodifiedtime.getDate(),"d"));

    // If X days old, delete
    var diff = dateDiff(dtNow,lastmodifiedtime.getDate(),"d");
    if (diff >= 5)
    {
    deleteFile(filename.getString());
    }
    }
    catch(err)
    {
    var warn="warning";
    }


    The above at least subtracts two dates but the delete still isn't working. I do have permission on the AS400 IFS.

    Thanks,
    David

  4. #4

    Default

    Fair enough.

    Try logging the error to a file with the appendToFile(var,var) JavaScript function to see what its complaining about. You can find that function in the "Special Functions" nav item and get an example by right-clicking on the function and choosing "Sample".

    Also, you can run JavaScript from the Windows Task Scheduler, if you wanted to go the route of using the OS.

    --Jeff

  5. #5

    Smile

    Jeff your "poor man's" logging worked great. Added a line feed and carriage return "/r/n" so the log wasn't
    all jammed together and sent the date difference first. Once that was straight, sent the path and file name. That was really what was screwing up because the path/filename I was sending back to the AS400 was unrecognizable. Ended up using deleteFile(uri.getString()); for that part.

    Thanks for teaching me!
    David

    For those that might travel this road, here's a bit of javascript that will help you delete files
    that are >= 10 days:

    // Determine file date and delete after X days old

    // Figure out today's date.
    var dtNow = new Date();
    var diff = dateDiff(lastmodifiedtime.getDate(), dtNow,"d");

    try
    {
    //var strFile = "C:/Temp/MyLog.txt";
    //var strContent = uri.getString();
    //appendToFile(strFile, strContent + "\r\n");

    // If X days old, delete
    if (diff >= 10)
    {
    deleteFile(uri.getString());
    }
    }
    catch(err)
    {
    var warn="warning";
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.