Using Intrinsic Constants
Microsoft® Windows® 2000 Scripting Guide
VBScript includes a number of intrinsic constants that can be used to construct message boxes, format output, or carry out other activities. To improve the readability of your scripts, you might want to use these intrinsic constants rather than their numeric equivalents.
For example, the following script sample uses a pair of numeric values to display a message box and then determine which button in that message box was clicked. Although the script works, anyone not familiar with VBScript values would have difficulty reading and editing the script. They must know that 260 means, "Create a message box with a Yes button and a No button, with the second button being the default button," and that the number 7 means, "The user clicked the No button."
ConfirmDelete = MsgBox ("Are you sure you want to delete these files?", _
260, "Delete all files")
If ConfirmDelete = 7 then
Wscript.Quit
End If
The following revised version of this script uses VBScript intrinsic constants (VbYesNo, VBDefaultButton2, and VbNo) instead of literal values. This makes the script easier to read and understand.
ConfirmDelete = MsgBox ("Are you sure you want to delete these files?", _
VbYesNo OR VBDefaultButton2, "Delete all files")
If ConfirmDelete = VbNo then
Wscript.Quit
End If
Using intrinsic constants also helps prevent your scripts from breaking each time the scripting language is updated. The VBScript constants are unlikely to change; there is little chance that the constant VbYesNo will be changed to something like VbNoYes. However, the value of those constants can conceivably change the next time VBScript is updated. Using constants can also make it easier to migrate scripts to another language should the need arise. For example, VBScript uses the value 1 to represent True. In Visual Basic .NET, however, True equals 1. It will be much easier to convert a script that uses the constant True than a script that uses the hard-coded value 1.
Most likely you will repeatedly find yourself using two intrinsic constants:
VbCrLf. This is equivalent to pressing the ENTER key and is typically used to format output for display. For example, this statement creates a message that displays a line of text, a blank line, and then a second line of text:
Wscript.Echo "This is the first line of text." & VbCrLF & VbCrLF & _ "This is the second line of text."
When the preceding script is run using Wscript, a message box similar to the one shown in Figure 2.11 is displayed.
Figure 2.11 Messages Separated by Using VbCrLf
VbTab. This is equivalent to pressing the TAB key. For example, this statement creates three tab-separated columns:
Wscript.Echo " 1" & VbTab & " 2" & VbTab & " 3" Wscript.Echo "A" & VbTab & "B" & VbTab & "C" Wscript.Echo "D" & VbTab & "E" & VbTab & "F"
When the preceding script runs under Cscript, the following output appears in the command window:
1 2 3 A B C D E F
Scripts written in VBScript have access only to the intrinsic constants defined in VBScript, and do not have native access to the intrinsic constants found in WMI, ADSI, the Script Runtime library, or other external Automation objects. When you write scripts using VBScript, you can use intrinsic VBScript constants such as VbCrLf or VbYesNo without defining those constants. However, to use intrinsic WMI or ADSI constants, you must explicitly define the value of those constants.
For example, the Script Runtime Drive object includes the intrinsic constant Fixed to indicate a fixed disk drive. Automation languages that have access to the Script Runtime constants can use Fixed without explicitly defining a value for it.
Because VBScript does not have access to this constant, any attempt to use this constant without first defining it will cause the script either to fail or to provide inaccurate information. For example, the following script will run, but it will not identify any fixed disks on your computer:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDiskDrives = objFSO.Drives
For Each objDiskDrive in colDiskDrives
If objDiskDrive.DriveType = Fixed then
Wscript.Echo objDiskDrive.DriveLetter
End if
Next
The script fails because VBScript does not know that Fixed is a constant that has the value 2. Instead of treating Fixed as a constant, VBScript treats Fixed as a variable. Until you assign a value to a variable, that variable is Empty. In this example, then, VBScript looks for disk drives where the DriveType property is equal to 0 rather than equal to 2. Because VBScript cannot find any drives with this property, the script does not return any data.
To make this script work, you must create your own constant named Fixed and explicitly assign it the value 2. This is shown in the following example:
Const Fixed = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDiskDrives = objFSO.Drives
For Each objDiskDrive in colDiskDrives
If objDiskDrive.DriveType = Fixed then
Wscript.Echo objDiskDrive.DriveLetter
End if
Next