Difference between revisions of "Windows Script Host"
Jump to navigation
Jump to search
Line 23: | Line 23: | ||
The function <code>zomg()</code> should be defined in zomg.js. | The function <code>zomg()</code> should be defined in zomg.js. | ||
+ | |||
+ | The following could be useful for running tests with cscript: | ||
+ | |||
+ | <package> | ||
+ | <job> | ||
+ | <script language="javascript"> | ||
+ | var stdin = WScript.StdIn; | ||
+ | var stdout = WScript.StdOut; | ||
+ | var stderr = WScript.StdErr; | ||
+ | </script> | ||
+ | <script language="javascript" src="test.js"></script> | ||
+ | </job> | ||
+ | </package> | ||
=Console I/O= | =Console I/O= |
Revision as of 07:16, 12 May 2005
Launching
At the console, type
cscript //nologo scriptname.js
The //nologo is optional; it suppresses the version output (which, for reasons unknown, is output on default!).
Non-simple scripts
To run something that encompasses more than one script file (that is, something that includes something else) or language, use a .wsf script. Here is what a typical one might look like:
<package> <job> <script language="javascript" src="zomg.js"></script> <script language="javascript"> zomg("Howdy"); </script> </job> </package>
The function zomg()
should be defined in zomg.js.
The following could be useful for running tests with cscript:
<package> <job> <script language="javascript"> var stdin = WScript.StdIn; var stdout = WScript.StdOut; var stderr = WScript.StdErr; </script> <script language="javascript" src="test.js"></script> </job> </package>
Console I/O
To write to stdout, use the WScript.StdOut object. For stderr, WScript.StdErr.
WScript.StdOut.Write("something"); WScript.StdErr.WriteLine("something that's a line");
WScript.StdIn, of course, corresponds to stdin.
var input = ""; while(!WScript.StdIn.AtEndOfLine) input += WScript.StdIn.Read(1); // read 1 character
var stdin = WScript.StdIn; var stdout = WScript.StdOut; while(!stdin.AtEndOfStream) { var str = stdin.ReadLine(); stdout.WriteLine("Line " + (stdin.Line - 1) + ": " + str); }