Share via


Variables

Microsoft® Windows® 2000 Scripting Guide

The script in Listing 2.2 works exactly as expected. When run, it reports the amount of free disk space on drive C. That does not mean that the script cannot be improved, however. For example, the FreeSpace property reports the number of bytes available on a drive. Because disk drive space is typically reported in gigabytes, the FreeSpace property often returns a value that is difficult to interpret. For example, Figure 2.1 shows the value reported for a drive with approximately 10 gigabytes of free disk space.

Figure 2.1 Free Disk Space Expressed in Bytes

sas_vbs_03s

Although it might be obvious from a glance that drive C has adequate disk space, it is far less obvious just how much disk space is actually available. System administrators might find it easier to interpret the data returned by this script if the data were reported as megabytes rather than bytes.

VBScript includes a full range of mathematical functions that enable you to perform such tasks as converting bytes to megabytes. In addition, VBScript also provides a construct - the - variable that can be used to store the results of those mathematical equations. In fact, variables provide a way to store any type of data while the script is running.

Variables represent portions of memory that are available to the script as it runs. You can think of computer memory, for these purposes, as being a series of cubbyholes. A variable would be one of these cubbyholes, with an identifying label attached. You can store any kind of data in this cubbyhole and VBScript will retrieve the data when it is needed. When you want to reference the data, VBScript simply looks up the memory address, and reports the information stored there.

In line 3 of Listing 2.3, a variable named FreeMegabytes is used to store the results of dividing FreeSpace by 10484576 (which converts bytes to megabytes). As soon as line 3 is run, the variable FreeMegabytes takes on the value of this equation. If you need to refer to the number of free megabytes of disk space anywhere else in the script, you do not have to repeat this equation; instead, you can simply reference the variable FreeMegabytes. This is shown in line 4, where the variable is echoed to the screen.

Listing 2.3 Using Variables

  
1
2
3
4
Set objWMIService = GetObject("winmgmts:")
Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'")
FreeMegaBytes = objLogicalDisk.FreeSpace / 1048576
Wscript.Echo FreeMegaBytes

When the script in Listing 2.3 runs, a dialog box similar to that shown in Figure 2.2 is displayed.

Figure 2.2 Free Disk Space Converted to Megabytes

sas_vbs_04s

Note

  • Notice that the equation used the number 1048576 instead of 1,048,576 (with commas separating the thousands). You cannot use a comma or any other character to separate thousands in VBScript. Instead, you must run all the digits together. This is true for numbers hard-coded into the script as well as for numbers that are entered as command-line arguments or in response to a prompt.

Formatting Script Output

The value 10340.4458007813 (meaning 10,340 megabytes of free space) is probably more meaningful to the typical system administrator than the value 10842824704. However, the numbers after the decimal point are more of a distraction than they are useful information. Fortunately, VBScript provides several different ways to modify script output. For example, the Int function causes VBScript to display only the integer portion of a number, leaving off all digits following the decimal point. The Int function is shown in line 4 of Listing 2.4.

Listing 2.4 Formatting Output

  
1
2
3
4
Set objWMIService = GetObject("winmgmts:")
Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'")
FreeMegaBytes = objLogicalDisk.FreeSpace / 1048576
Wscript.Echo Int(FreeMegaBytes)

When the script in Listing 2.4 runs, a dialog box similar to that shown in Figure 2.3 appears. The Int function strips away all the digits following the decimal point, leaving only the integer portion of the original value. Additional formatting commands (covered later in this chapter) can be used to add a comma to the output, resulting in the displayed value 10,340.

Figure 2.3 Using the Int Function to Format Output

sas_vbs_05s