Solved

Java Script: Output to file

Posted on 2011-03-19
31
526 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:ReneGe
  • 16
  • 15
31 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 35174388
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35174399
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35174864
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35174888
"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
 
LVL 10

Author Comment

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

Thanks for helping.

Cheers,
Rene
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175287
Does the script you ran beforehand look exactly like you posted it ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175406
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35175708
-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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175780
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175790
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35175835
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35175841
My response was after your previous comment, not your last one. I will now test your script from your last comment.

Cheers,
Rene
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175846
I would expect
http://www.google.com

as an argument
0
 
LVL 10

Author Comment

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

0
 
LVL 10

Author Comment

by:ReneGe
ID: 35175868
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175871
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35175885
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35175903
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35175908
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35175930
I guess we'r in a same kind of boat.
What's you time zone? Me it's "-5GMT"
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35176051
GMT+1
0
 
LVL 10

Author Comment

by:ReneGe
ID: 35191742
mplungjan,

Are you still on it?

Cheers,
Rene
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35192185
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35192485
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
 
LVL 10

Author Closing Comment

by:ReneGe
ID: 35192517
Even if this issue was not resolved here, your contribution have been greatly helpful and appreciated.

Thanks and cheers,
Rene
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35192540
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
 
LVL 10

Author Comment

by:ReneGe
ID: 35192666
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35192798
Ok, I'll give it another go tomorrow
0
 
LVL 10

Author Comment

by:ReneGe
ID: 35192923
Cool
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35204722
Sorry. I did not get any results from my inquiries.
I suggest you ask the question again in the scripting zone
0
 
LVL 10

Author Comment

by:ReneGe
ID: 35206069
Dont worry

Thanks a lot for everything.

Cheers,
Rene
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now