Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Although the command window offers little in the way of formatting options, it is possible to at least align output in table format. Simply displaying multiple properties one after another, all on the same line, usually results in confusion; who really knows what to make of a command window display such as this:
Alerter Auto Running Application Management Manual Running Ati HotKey Poller Auto Stopped Computer Browser Auto Running Indexing Service Manual Stopped
However, that same data, displayed in the same command window, is remarkably easy to read and interpret when displayed in tabular format:
Alerter Auto Running Application Management Manual Running Ati HotKey Poller Auto Stopped Computer Browser Auto Running Indexing Service Manual Stopped
To display data in tabular format, you need to use fixed-width columns. With fixed-width columns, you determine in advance the number of characters that will be displayed in each column in the table. In the preceding example, the following column widths based on a command window 80 characters wide are used:
After you decide on the widths for each column, you must then ensure that each item displayed in the table takes up the requisite number of spaces. This is done by using the following procedure on each item of data:
Len(
"dog
")
returns the value 3 because there are three characters in the word dog.The net result is that each item of data expands to fill the column width, even if many of the characters in that item are blank spaces. For example, the word dog expands to 10 characters (the underscores represent blank spaces in the string):
d o g -------
A subset of VBScript functions that can be used to format data for display in the command window are shown in Table 17.2. In addition to the functions listed in the table, VBScript also includes functions you can use to display numbers and dates in a specific manner. For more information about these functions (such as FormatNumber, FormatDate, and FormatPercent), see "VBScript Primer" in this book. The discussion of formatting output used in the "VBScript Primer" chapter is similar to the discussion in this chapter.
Table 17.2 VBScript String Formatting Functions
Function | Description |
---|---|
Len | Returns the number of characters in a string. For example, this code returns the value 15, because "This is a test." contains 15 characters:
|
Left | Returns the number of specified characters beginning from the left of a string. For example, this code returns the string "This is", the first seven characters in the test string: strTestString = "This is a test. " strCharacters = Left(strTestString, 7) Wscript.Echo strCharacters |
Right | Returns the number of specified characters beginning from the right of a string and working backward. For example, this code returns the string " a test.", the last seven characters in the test string:
|
Space | Appends the specified number of spaces to the end of the string. For example, this code returns the string "This is a test.---------------", with the hyphens representing the 15 blank spaces appended to the end of the string: strTestString = "This is a test. " strCharacters = strTestString & Space(15) Wscript.Echo strCharacters |
Listing 17.5 contains a script that displays formatted script output in a command window. To carry out this task, the script must perform the following steps:
This query returns a collection consisting of all the services installed on the computer.
This is done by setting the variable intPadding to 50 (the number of spaces in the first column of the display) minus the length of the service display name (determined by using the Len function). For example, if the service display name contains 23 characters, intPadding is equal to 27 (50 23).
This is done by setting the variable intPadding2 to 17 (the number of spaces in the second column of the display) minus the length of the service start mode (determined by using the Len function). For example, if the start mode contains 4 characters (such as a start mode of Auto), intPadding is equal to 13 (17 4).
To do this, set strDisplayName to the display name of the service, and then use the Space function to add the proper number of blank spaces. The proper number of blank spaces is configured by using intPadding as the parameter for the Space function.
The net result of using the Space function will be similar to this (the hyphens indicate blank spaces added to the service name):
Application Management
Application Management-----------------------------
Listing 17.5 Displaying Tabular Output in a Command Window
|
|
When the script runs under CScript, it displays formatted output similar to the following:
Alerter Auto Running
Application Management Manual Running
Ati HotKey Poller Auto Stopped
Computer Browser Auto Running
Indexing Service Manual Stopped
Send us your feedback | « Previous | Next » |