Difference between revisions of "Windows Script Host"

From HalfgeekKB
Jump to navigation Jump to search
Line 48: Line 48:
 
   
 
   
 
         </script>
 
         </script>
        <script language="javascript" src="hxcore.js"></script>
 
        <script language="javascript" src="hxbytec.js"></script>
 
 
         <script language="javascript" src="test.js"></script>
 
         <script language="javascript" src="test.js"></script>
 
     </job>
 
     </job>
 
  </package>
 
  </package>
 
  
 
=Console I/O=
 
=Console I/O=

Revision as of 07:25, 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;

            function print_array(args) {
                for(var i=0; i<args.length; ++i)
                    WScript.StdOut.Write(args[i]);
            }

            function print() {
                print_array(arguments);
            }

            function echo() {
                print_array(arguments);
                print_array(new Array("\n"));
            }

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

See also