Displaying Tabular Script Output in a Command Window

Microsoft® Windows® 2000 Scripting Guide

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:

  • Column 1: 50 characters
  • Column 2: 17 characters
  • Column 3: 13 characters

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:

  1. Determine the number of characters in the item. The Len function is used to return the number of characters in a string. For example, the code Len("dog") returns the value 3 because there are three characters in the word dog.
  2. Subtract the number of characters in the string from the predetermined column width. This tells you how many additional character spaces must be added to the string to fill out the column. For example, if the word dog is to be displayed in a column 10 characters wide, you subtract the number of characters in dog (3) from the column width (10).
  3. Use the Space function to append blank spaces to the end of the string, expanding the string to fill the entire column. In the example shown in step 2, you need to append seven spaces to the end of the word dog. Because blank spaces count as characters, adding seven spaces makes the string ten characters long: the letters d, o, and g, and the seven blank spaces.

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

FunctionDescription
LenReturns the number of characters in a string. For example, this code returns the value 15, because "This is a test." contains 15 characters:
strTestString = "This is a test." intCharacters = Len(strTestString) Wscript.Echo intCharacters
LeftReturns 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
RightReturns 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:
strTestString = "This is a test." strCharacters = Right(strTestString, 7) Wscript.Echo strCharacters
SpaceAppends 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

Scripting Steps

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:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  3. Use the ExecQuery method to query the Win32_Service class.

    This query returns a collection consisting of all the services installed on the computer.

  4. For each service in the collection, determine the number of spaces that must be appended to the service display name to ensure that the name takes up 50 spaces.

    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).

  5. Determine the number of spaces that must be appended to the service start mode to ensure that the name takes up 17 spaces.

    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).

  6. Set the variable strDisplayName to the service display name plus enough blank spaces to make the display name take up 50 characters.

    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-----------------------------

  7. Set the variable strStartMode to the service start mode, plus enough blank spaces to make the start mode occupy 17 characters.
  8. Echo strDisplayName, strStartMode, and the service state.

Listing 17.5   Displaying Tabular Output in a Command Window

1 2 3 4 5 6 7 8 9 10 11 12 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") Set colServices = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service") For Each objService in colServices     intPadding = 50 - Len(objService.DisplayName)     intPadding2 = 17 - Len(objService.StartMode)     strDisplayName = objService.DisplayName & Space(intPadding)     strStartMode = objService.StartMode & Space(intPadding2)     Wscript.Echo strDisplayName & strStartMode & objService.State Next

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 Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net