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

LVL 10
ReneGeAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Michel PlungjanIT ExpertCommented:
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 );
}
0
 
ReneGeAuthor Commented:
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
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Michel PlungjanIT ExpertCommented:
"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.

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

Thanks for helping.

Cheers,
Rene
0
 
Michel PlungjanIT ExpertCommented:
Does the script you ran beforehand look exactly like you posted it ?
0
 
Michel PlungjanIT ExpertCommented:
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
0
 
ReneGeAuthor Commented:
-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
0
 
Michel PlungjanIT ExpertCommented:
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 );
0
 
Michel PlungjanIT ExpertCommented:
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

0
 
ReneGeAuthor Commented:
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



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

Cheers,
Rene
0
 
Michel PlungjanIT ExpertCommented:
I would expect
http://www.google.com

as an argument
0
 
ReneGeAuthor Commented:
OUTPUT:
input: www.google.com
output: www.google.com.txt
WebsiteMonitor.js(23, 1) (null): Unspecified error

0
 
ReneGeAuthor Commented:
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
0
 
Michel PlungjanIT ExpertCommented:
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

0
 
ReneGeAuthor Commented:
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

0
 
ReneGeAuthor Commented:
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
0
 
Michel PlungjanIT ExpertCommented:
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.
0
 
ReneGeAuthor Commented:
I guess we'r in a same kind of boat.
What's you time zone? Me it's "-5GMT"
0
 
Michel PlungjanIT ExpertCommented:
GMT+1
0
 
ReneGeAuthor Commented:
mplungjan,

Are you still on it?

Cheers,
Rene
0
 
Michel PlungjanIT ExpertCommented:
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?
0
 
ReneGeAuthor Commented:
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
0
 
ReneGeAuthor Commented:
Even if this issue was not resolved here, your contribution have been greatly helpful and appreciated.

Thanks and cheers,
Rene
0
 
Michel PlungjanIT ExpertCommented:
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?
0
 
ReneGeAuthor Commented:
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
0
 
Michel PlungjanIT ExpertCommented:
Ok, I'll give it another go tomorrow
0
 
ReneGeAuthor Commented:
Cool
0
 
Michel PlungjanIT ExpertCommented:
Sorry. I did not get any results from my inquiries.
I suggest you ask the question again in the scripting zone
0
 
ReneGeAuthor Commented:
Dont worry

Thanks a lot for everything.

Cheers,
Rene
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.