Tuesday, April 24, 2012

Echo Method

Outputs text to either a message box or the command console window.
object.Echo [Arg1] [,Arg2] [,Arg3] ... 
 
 object 
WScript object. 
Arg1, Arg2, Arg3 ...
Optional. String value indicating the list of items to be displayed.
The Echo method behaves differently depending on which WSH engine you are using.

WSH engineText Output
Wscript.exegraphical message box
Cscript.execommand console window
Each displayed item is separated with a space character. When using CScript.exe, each item is displayed with a newline character. If no items are provided as arguments to the Echo method, a blank line is output.

Example:

The following example uses the Echo Method to display the domain name, computer name, and user name for the current machine, and to display network mapping information for the drives and printers.

    language="VBScript"
         Set WshNetwork = WScript.CreateObject("WScript.Network")
         Set oDrives = WshNetwork.EnumNetworkDrives
         Set oPrinters = WshNetwork.EnumPrinterConnections
         WScript.Echo "Domain = " & WshNetwork.UserDomain
         WScript.Echo "Computer Name = " & WshNetwork.ComputerName
         WScript.Echo "User Name = " & WshNetwork.UserName
         WScript.Echo 
         WScript.Echo "Network drive mappings:"
         For i = 0 to oDrives.Count - 1 Step 2
            WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
         Next
         WScript.Echo 
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
            WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
         Next
      

        language="JScript"
         var WshNetwork = WScript.CreateObject("WScript.Network");
         var oDrives = WshNetwork.EnumNetworkDrives();
         var oPrinters = WshNetwork.EnumPrinterConnections();
         WScript.Echo("Domain = " + WshNetwork.UserDomain);
         WScript.Echo("Computer Name = " + WshNetwork.ComputerName);
         WScript.Echo("User Name = " + WshNetwork.UserName);
         WScript.Echo();
         WScript.Echo("Network drive mappings:");
         for(i=0; i< oDrives.Count(); i+=2){
            WScript.Echo("Drive " + oDrives.Item(i) + " = " + oDrives.Item(i+1));
         }
         WScript.Echo();
         WScript.Echo("Network printer mappings:");
         for(i=0; i< oPrinters.Count(); i+=2){
            WScript.Echo("Port " + oPrinters.Item(i) + " = " + oPrinters.Item(i+1));
         }
      
 Equivalent using the property ".Stdout" as a TextStream                 
The following :
       WScript.Echo Args
is mostly equivalent to:
    WScript.Stdout.WriteLine Args
and give you access to other methods like
    Wscript.StdOut.Write Args
...except that the Stdout property is empty in Wscript (or in Internet Browser's scripts), where the Echo method uses replace it with a MessageBox (Alert in Javascript/ECMAScript). However, in CScript, the Stdout property allows better control of the output, and does not force you to output a newline break, and allows formatting its output (see also the TextStream class).

 Given that WSH does not offer any way to create TextStreams other than though the exposed StdIn, Stdout properties of the host (used internally by the Echo method), and through the FileSystemObject (FSO) class (by opening a file), the only way to format a string for display in a MessageBox compatible with the very limited Echo method in WScript, will be to use complex string formatting code ! Unfortunately, WSH still lacks support for TextStreams with output at least in memory (into a String object), in such a way that it would work transparently between CScript and Wscript with scripts written in JScript/ECMAScript or VBScript., and with the same facilities offered in VB for Applications and other .Net languages (C#, J#, ...). The workaround (using a FSO to create a temporary file or pipe then rereading it) is very inefficient (and also restricted due to FSO security restrictions, notably within the context of a browser). WSH engines should expose at least a method allowing to create a TextStream writing into a referenced String buffer (via concatenation).

No comments: