[Previous] [Table of Contents] [Next]

Using the Echo Method

In previous chapters, I used the Echo method to create a dialog box in WSH. You can use the Echo method in either VBScript or JScript. This method has the following syntax:

WScript.Echo Parameters

WSH automatically exposes the WScript object to the running script, and the WScript object exposes the Echo method. You can use the Echo method in a statement such as the following:

WScript.Echo "Hello, world"

You can call the Echo method with one or more parameters. Use commas to separate multiple parameters, as shown here:

WScript.Echo "Hello,", "world"

These parameters pass the information displayed in the dialog box. The parameters can contain values of different data types. For example, the first parameter can contain a string value, the second parameter can contain a number, and so on. Echo parses the parameters, converts their values to strings, and shows the results in the dialog box. Echo automatically inserts blanks between the values in the dialog box.

NOTE
In Chapter 3, I mentioned that the Echo method is a procedure call—that is, it doesn't return a value. Therefore, in VBScript you must pass parameters to the Echo method without using parentheses, as in WScript.Echo "Hello, world". In JScript, you always enclose parameters in parentheses, as in WScript.Echo ("Hello, world");.

Using Echo in VBScript

Listing 6-1 uses the Echo method of the WScript object to display a simple dialog box in VBScript. (This sample file, like most of the others used in this chapter, is in the \WSHDevGuide\Chapter06 folder on the book's companion CD.)

Listing 6-1 Echo.vbs

'************************************************
' File:    Echo.vbs (WSH sample in VBScript) 
' Author:  (c) G. Born
'
' Using the Echo method in VBScript
'************************************************
Option Explicit

' Declare several variables.
Dim price, vat, net

vat = 16.0
net = 100.0

' Calculate price.
price = net * (1.0 + vat/100.0)

' Display results. Because the WScript (application) object is 
' automatically available, no further steps are necessary.
WScript.Echo "Price: ", price, "US $ Tax: ", vat, _
             "% ", price - net, " US $"

'*** End

The program in Listing 6-1 uses a net price, applies a value-added tax (VAT) rate, and shows the results in a dialog box. The net price and the VAT are declared as the variables net and vat using the Dim statement in the program header. The values are then assigned to the variables. Next, a simple calculation is made to get the final price and store it in the variable price. The result is shown in a dialog box using the Echo method.

WScript.Echo "Price: ", price, "US $ Tax: ", vat, _
             "% ", price - net, " US $"

The parameters are separated with commas. The preceding lines also contain a continuation character (_) to split the command into two lines. The result of this statement is shown in Figure 6-1.

Figure 6-1 A dialog box created using the Echo method

NOTE
Recall from Chapter 3 that the dot is used to separate the object name, WScript, from the method name, Echo. This notation is also used to separate the object name from a property name.

Using Echo in the Command Prompt Window

You can also launch a script in the Command Prompt window in Microsoft Windows. If you use CScript.exe as the host, the Echo method sends all output to the Command Prompt window, as shown in Figure 6-2. Because CScript.exe is a console-oriented application, it doesn't show a dialog box.

Click to view at full size.

Figure 6-2 Echo output in the Command Prompt window

You can change where the output is sent if you need to write results obtained by a script into a file (for example, as a kind of logging). Console applications such as CScript.exe write to the stdout device, which sends all output to the Command Prompt window by default. However, you can use any of the stdout redirection operators (>, >>, and |) to send the output elsewhere. For example, you can redirect the Echo output to a text file by using the following command:

CScript.exe //NoLogo Echo.vbs > logfile.txt

This command does two things. The //NoLogo switch suppresses the two logo lines shown in the Command Prompt window after the scripting host starts. (See Chapter 1 for more detailed information about CScript.exe switches.) The redirection > logfile.txt causes stdout to write all program output created with Echo into the file logfile.txt instead of displaying it in the Command Prompt window. To append the output to an existing file, you must use the >> characters for redirection. Figure 6-3 shows the Command Prompt window with the command for redirecting the script output to a text file; the Notepad window in the foreground shows the content of the text file.

Click to view at full size.

Figure 6-3 Redirecting Echo output in the Command Prompt window and viewing the result in Notepad

Using Echo in JScript

In JScript, using the Echo method is the only way to show a dialog box without creating additional objects (because WSH automatically exposes the WScript object). You saw the dialog box displayed by the Echo method in Figure 6-1. Listing 6-2 uses the Echo method in JScript. (Remember that you must enclose the parameters in parentheses and that the statement must be terminated with a semicolon.)

Listing 6-2 Echo.js

//************************************************
// File:    Echo.js (WSH sample in JScript) 
// Author:  (c) G. Born
//
// Using the Echo method in JScript
//************************************************
// Declare variables.
var price;
var vat = 16.0;
var net = 100.0;

// Calculate price.
price = net * (1.0 + vat/100.0);

// Show results. Because we're using the WScript object, we don't need
// to create the WScript object.
WScript.Echo("Price: ", price, "US $ Tax: ", vat, 
             "% ", price - net, " US $");

//*** End

Listing 6-2 shows once again how you can use a method in a script. The Echo statement uses the WScript object. A dot separates the object name and the method name. The Echo method uses several parameters for the results to be displayed. All parameters are separated with commas. Numeric values in a parameter are converted automatically by the Echo method to strings. The title text of the dialog box and the button for closing the dialog box are created by the method itself. (Later you'll learn how to define your own title text.)

Using a Line Feed with Echo

I've been asked many times how you can force the Echo method (or other methods that I'll mention later) to wrap a string onto several lines. If you pass text to the Echo method, the run-time system does the formatting and line wrapping automatically. Parameters are separated with spaces within the dialog box window. A long piece of text is wrapped onto multiple lines, but short text is shown on one line.

Unfortunately, the Echo method (and the MsgBox function discussed in the next section) doesn't provide an explicit option to force a line feed during output. But as you can see in Figure 6-4, you can use a line feed in the dialog box. So how do you force the lines to break?

Figure 6-4 Text on multiple lines in a dialog box

The solution is rather simple: you insert the code for a new line into the text stream. The implementation is different in JScript and VBScript. JScript has several escape sequences for embedding special control codes into a string. You force a new line with the escape sequence "\n". If you need to direct the output of the Echo method onto several lines, you insert "\n" into the parameters. (See Table 5-1 for a list of all the escape sequences for JScript strings.) The following statement uses this approach to divide the text into two lines:

WScript.Echo("Price: ", price, "US $ \nTax: ", vat, 
             "% ", price - net, " US $");

Alternatively, you can set the escape string as a separate parameter:

WScript.Echo("Price: ", price, "US $", "\n", "Tax: ", vat, 
             "% ", price - net, " US $");

VBScript doesn't recognize the "\n" escape sequence. Instead, it offers the predefined named constant vbCrLf, which contains the character codes to force a new line. In VBScript, the Echo method with a line feed can be written as follows:

WScript.Echo "Price: ", price, "US $" & vbCrLf & _
             "Tax: ", vat, "% ", price - net, " US $"

The & character concatenates the named constant vbCrLf with the other substrings. You can also insert vbCrLf as a separate parameter, like this:

WScript.Echo "Hello,", vbCrLf, "world"