Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
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
Free Disk Space Expressed in Bytes
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
|
|
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
Free Disk Space Converted to Megabytes
Note
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
|
|
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
Using the Int Function to Format Output
Send us your feedback | « Previous | Next » |