Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

Java Script: Output to file

Hi there,

I need to modify the included script so it output's to a file by it self, not through the batch file.

It is currently called from a batch file using cscript as follows:
cscript websitemonitor.js "www.websitetomonitor.com">websitetomonitor.com.txt

The new script will be called like this and the output file will be presented as an argument:
cscript websitemonitor.js "www.awebsite.com" "awebsite.com.txt"

Thanks for your help,
Rene

 
var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);
notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo("objArgs(0)");
request.open( "GET", "objArgs(0)" , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i.e something like

var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);

// Instantiate a File System ActiveX Object:
 var fso = new ActiveXObject("Scripting.FileSystemObject");
// Invoke the method:
var a = fso.CreateTextFile(objArgs(1), true);
// Do something with it:
a.Write(request.responseText);
// Close the connection:
a.Close();


notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo("objArgs(0)");
request.open( "GET", "objArgs(0)" , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}
Avatar of ReneGe

ASKER

mplungjan,

Your script does not work

Since I don't know Java Script.  Please provide me with the complete solution, not suggestions.

Thanks and cheers,
Rene
"does not work" is not very helpful :(
Can you tell me if you get any error messages, and if so, which ones. Is the folder you run the script have write permissions

Since I do not have access to a windows PC until tomorrow, I could not test the script and it was not obvious from your question that you did not know JS.

I will try testing this tomorrow unless someone comes along before with a working solution.

Avatar of ReneGe

ASKER

Here is the error message:
WebsiteMonitor.vbs(1, 32) Microsoft VBScript compilation error: Expected end of statement.

Thanks for helping.

Cheers,
Rene
Does the script you ran beforehand look exactly like you posted it ?
vbs is not js. So why do you call it vbs? You used to call it .js which is javascript. vbs is visualbasic and will give errors if it contains javascript syntax
Avatar of ReneGe

ASKER

-Does the script you ran beforehand look exactly like you posted it ? = Yes
-vbs is not js. So why do you call it vbs? = My mistake. Sorry for that.

I changed the extension of the script for "js" and here is the error message:
WebsiteMonitor.vbs(26,1) (null): Unspecified error

Thanks for your dedicated help,
Rene
But you still callit .vbs or the error would say
WebsiteMonitor.js(26,1) (null): Unspecified error

No?

Also the error is in the old code, which I now see could never work since you have quoted the variable that should hold the input file.

request.open( "GET", "objArgs(0)" , true );
should be
request.open( "GET", objArgs(0) , true );
Try this and note the output
var objArgs = WScript.Arguments;
WScript.Echo("input: "+objArgs(0));
WScript.Echo("output: "+objArgs(1));

var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = true;

request.onreadystatechange=function() {
  if(request.readyState==4) {
    // WScript.Echo(request.responseText);
    // Instantiate a File System ActiveX Object:
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    // Invoke the method:
    var a = fso.CreateTextFile(objArgs(1), true);
    // Do something with it:
    a.Write(request.responseText);
    // Close the connection:
    a.Close();
    notyetready = false;
  }
}

request.open( "GET", objArgs(0) , true );
request.send(null);

while( notyetready ) {
  WScript.Sleep( 100 );
}

Open in new window

Avatar of ReneGe

ASKER

I'm so sorry. I did not speep a lot the last 3 weeks so I tend to easely make typo mistake.

I re-wrote the error message by hand and made a finger glitch. It is indeed ".js".

I tried your sugggested code modification and got the same error message:
WebsiteMonitor.js(26,1) (null): Unspecified error

Here is my current test command line:
cscript //nologo "WebsiteMonitor.js" "www.google.com" "www.google.com.txt"

Thanks,
Rene

 
var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = 1;

request.onreadystatechange=function()
{
if(request.readyState==4)
{
WScript.Echo(request.responseText);

// Instantiate a File System ActiveX Object:
 var fso = new ActiveXObject("Scripting.FileSystemObject");
// Invoke the method:
var a = fso.CreateTextFile(objArgs(1), true);
// Do something with it:
a.Write(request.responseText);
// Close the connection:
a.Close(); 


notyetready = 0;
}
}

var objArgs = WScript.Arguments;
WScript.Echo("objArgs(0)");
request.open( "GET", objArgs(0) , true );
request.send(null);

while( notyetready )
{
WScript.Sleep( 100 );
}

Open in new window



Avatar of ReneGe

ASKER

My response was after your previous comment, not your last one. I will now test your script from your last comment.

Cheers,
Rene
I would expect
http://www.google.com

as an argument
Avatar of ReneGe

ASKER

OUTPUT:
input: www.google.com
output: www.google.com.txt
WebsiteMonitor.js(23, 1) (null): Unspecified error

Avatar of ReneGe

ASKER

With:
cscript //nologo "WebsiteMonitor.js" "http://www.google.com" "www.google.com.txt"

I do not get any errors, the output file is created but is empty
Here is one with more details (even more info at Microsoft)
var objArgs = WScript.Arguments;
WScript.Echo("input: "+objArgs(0));
WScript.Echo("output: "+objArgs(1));

var request = new ActiveXObject("Msxml2.XMLHTTP");
var notyetready = true;

request.onreadystatechange=function() {
  if (request.status >= 400 && request.status <= 599){
    WScript.Echo("Error Occurred : " + request.status + " - " + request.statusText);
    return;
  }

  if(request.readyState==4) {
    WScript.Echo(request.responseText);
    // Instantiate a File System ActiveX Object:
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    // Invoke the method:
    var a = fso.CreateTextFile(objArgs(1), true);
    // Do something with it:
    a.Write(request.responseText);
    // Close the connection:
    a.Close();
    notyetready = false;
  }
}

request.open( "GET", objArgs(0) , true );
request.send(null);



while( notyetready ) {
  WScript.Sleep( 100 );
}

Open in new window

Avatar of ReneGe

ASKER

OUTPUT:

input: http://www.google.com
output: www.google.com.txt

WebsiteMonitor.js(9, 3) msxml3.dll: Unspecified error

WebsiteMonitor.js(9, 3) msxml3.dll: Unspecified error

Avatar of ReneGe

ASKER

Would you be more comfortable doing this in VBscript? If yes, well i'd actually prefer having this script in VBscript.

Thanks and cheers,
Rene
No way! I do JS, but rarely WSH

hard since I cannot test it myself until tomorrow.

Which I will - some 10 hours from now.

Now time for the family.
Avatar of ReneGe

ASKER

I guess we'r in a same kind of boat.
What's you time zone? Me it's "-5GMT"
Avatar of ReneGe

ASKER

mplungjan,

Are you still on it?

Cheers,
Rene
Drat. I was, but gave up on your script and wanted to show it as an HTA

Do you have to do it from the command line or is it ok to have a small gui?
Avatar of ReneGe

ASKER

I must not have a GUI.

I found a alternative solution.

I will close this thread and give you your points for your efforts and help.

Thanks and cheers,
Rene
Avatar of ReneGe

ASKER

Even if this issue was not resolved here, your contribution have been greatly helpful and appreciated.

Thanks and cheers,
Rene
Hey, I am not done.

I'll look again tomorrow if you do not mind. But can I give you a gui instead of a commandline script?
Avatar of ReneGe

ASKER

This script is executed from a batch file and must not have a GUI. However, if you are up to the challange, that's fine with me.

Thanks again and cheers,
Rene
Ok, I'll give it another go tomorrow
Avatar of ReneGe

ASKER

Cool
Sorry. I did not get any results from my inquiries.
I suggest you ask the question again in the scripting zone
Avatar of ReneGe

ASKER

Dont worry

Thanks a lot for everything.

Cheers,
Rene