Windows Script Host

From HalfgeekKB
Revision as of 03:34, 12 May 2005 by 161.253.9.181 (talk)
Jump to navigation Jump to search

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.

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);
}

See also