Freigeben über


Concatenating Strings

Microsoft® Windows® 2000 Scripting Guide

Concatenation is the process of combining two or more strings into a single string. (You can also combine strings with numeric or date values.) Concatenation is often used to provide more readable or more meaningful output. For example, the script in Listing 2.4 returns the value 10340. This is very useful information, provided you know that the script is designed to return the number of megabytes of free disk space on drive C. But if you do not know what the script is designed to do, that output will be meaningless.

Among other things, concatenation helps you provide context for your script output. For example, rather than displaying the value 10340, you might want to display a message similar to "There are 10340 megabytes of free disk space." To do this, you must combine the following three items:

  • "There are " a simple string representing the start of the message.

  • FreeMegabytes the variable containing the number of free megabytes on the drive.

  • " megabytes of free disk space." a second string representing the end of the message.

As shown in lines 6 and 7 of Listing 2.7, you concatenate items in VBScript by using the ampersand (&).

Listing 2.7 Concatenating Strings

  
1
2
3
4
5
6
7
Const CONVERSION_FACTOR = 1048576
Computer = "atl-dc-01"
Set objWMIService = GetObject("winmgmts://" & Computer)
Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'")
FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
Wscript.Echo "There are " & Int(FreeMegaBytes) & _
 " megabytes of free disk space."

Note

  • The underscore (_) at the end of line 6 is known as the line continuation character and is used to indicate a statement break. This means that lines 6 and 7 should be treated as one line; the line was simply too long to fit in the allotted space. Statement breaks are covered in more detail later in this chapter.

Alternatively, you might have assigned the value "There are " to a variable named MessageStart and the value "megabytes of free disk space." to a variable named MessageEnd. You could then have concatenated the three variables like this:

Wscript.Echo MessageStart & Int(FreeMegabytes) & MessageEnd

If you look closely at lines 6 and 7, you will notice that blank spaces were hard-coded into the string values "There are " and " megabytes of free disk space." This is required because the ampersand does not insert any spaces between the items being concatenated. For example, suppose you leave out the blank spaces, like this:

Wscript.Echo "There are" & Int(FreeMegaBytes) & "megabytes of free disk space."

In this case, the resulting message box will run the three values together, as shown in Figure 2.6.

Figure 2.6 Incorrectly Concatenating String Values

sas_vbs_07s

For simple forms of concatenation, you can work around this problem by using a comma rather than an ampersand when combining the values:

Wscript.Echo "There are", Int(FreeMegaBytes), "megabytes of free disk space."

When the items are separated by a comma, a blank space is automatically inserted between the items. As a result, the message box is properly formatted, as shown in Figure 2.7.

Figure 2.7 Correctly Concatenating String Values

sas_vbs_08s