none
JScript to download file from IE - running into an issue RRS feed

  • Question

  • Hi Folks -

    I have the following Batch/JScript hybrid script that was working in the past but has since broke.  It broke because they added a new 'tr' section so I updated the reference accordingly from 2 to 3.

    This script is able to find the link, but having trouble downloading. Can anyone see what's wrong?

    @if (@a)==(@b) @end /* Batch part:
    @ECHO off &SETlocal
    
    CSCRIPT //nologo //e:jscript "%~fs0" "ME_PS_PATH=C:\ManageEngine\ServiceDesk\Patch_Sets"
    
    pause
    GOTO :EOF
    
    JScript Part : */
    
    var objIE = null;
    try {
      WScript.ECHO('Searching link ...');
      objIE = new ActiveXObject('InternetExplorer.Application');
      objIE.Visible = true;
      objIE.Navigate('https://www.manageengine.com/products/service-desk/service-packs.html');
      while (objIE.Busy) { WScript.Sleep(100); }
      var link = objIE.document.getElementsByClassName('box-table mT20')[0].getElementsByTagName('tr')[3].getElementsByTagName('td')[2].getElementsByTagName('a')[0].getAttribute('href');
      objIE.Quit();
      objIE = null;
      WScript.ECHO('Found: ' + link);
    
      WScript.ECHO('Downloading ...');
      var objXMLHTTP = new ActiveXObject('MSXML2.ServerXMLHTTP');
      objXMLHTTP.open('GET', link, false);
      objXMLHTTP.send();
    
      var objADOStream = new ActiveXObject('ADODB.Stream');
      objADOStream.Type = 1;
      objADOStream.Mode = 3;
      objADOStream.Open();
      objADOStream.Write(objXMLHTTP.responseBody);
      objADOStream.Position = 0;
    
      WScript.ECHO('Saving ...');
      var objFSO = new ActiveXObject('Scripting.FileSystemObject');
      objADOStream.SaveToFile(objFSO.BuildPath(WScript.Arguments(0), objFSO.GetFileName(link)), 2);
      objADOStream.Close();
    }
    catch(e) {
      if (objIE != null) { objIE.Quit(); }
      WScript.ECHO('Error!');
    }

    Thank you!

    Saturday, June 13, 2020 10:26 AM

All replies

  • If it achieves the “Saving…” section, did you check if the destination path (returned by BuildPath) is valid? Or specify some explicit path for testing.

    Try determining the error details. (Maybe add ‘WScript.ECHO(e)’ or ‘WScript.ECHO(e.message)’).

    Saturday, June 13, 2020 5:46 PM
  • Hi -

    No, it doesn't achieve the saving section because the Download section is failing. And yes, the path exists as this has been working in the past.

    The error says, "[Object Error]" and that's it. ANy idea?


    • Edited by cdtakacs1 Sunday, June 14, 2020 2:06 PM
    Sunday, June 14, 2020 2:04 PM