Choosing a Scripting Language In Windows XP
Windows Script Host doesn’t care whether you use VBScript, JScript, or some other scripting language. All the objects are available to any language, and in most situations, you can choose to use the language with which you are most comfortable. In this book, we use VBScript. You can actually mix languages in the same program.
VBScript is a major subset of Visual Basic. If you know Visual Basic, there is little that you can’t do in VBScript. One of the biggest differences is that VBScript has only Variant variables. You simply use Dim and the variable name; you can’t add a variable type because they are all the same. You can find documentation of VBScript at http://msdn.microsoft.com/library/en...riVBScript.asp.
Here is a short script, called Folders.vbs, that shows some of the elements of a script written in VBScript:
Option Explicit
Dim objWSShell
Dim strMsg
Dim intCtr
Set objWSShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "Your desktop is " & objWSShell.SpecialFolders("Desktop") _
& vbNewLine
strMsg = "All your special folders are:" & vbNewLine
For intCtr = 0 To objWSShell.SpecialFolders.Count - 1
strMsg = strMsg & objWSShell.SpecialFolders.Item(intCtr) & vbNewLine
Next
WScript.Echo strMsg
The script starts with an Option Explicit statement, which tells the VBScript interpreter to need you to use a Dim statement for every changeable in the script. This helps to avoid errors from misspelled variable names and is accepted as good programming practice.
The Set statement creates a WScript Shell object, which is used to access the special folders. One of the properties of the Shell object is the SpecialFolders object. The SpecialFolders object lets you retrieve the path and file name of any of the special folders on your system. For example, this script retrieves the path specification of the Desktop folder, as shown in Figure 1 below.
Figure 1. Folders.vbs initially displays the location of your Desktop folder.
The next section of the script, the For…Next loop, displays the folder paths of all the special folders. This section demonstrates a technique that makes scripts work well with both Wscript and Cscript. Instead of putting a WScript.Echo statement inside the loop, we add each result to the strMsg string and then display this string after the loop is finished. When run with Cscript, the results are the same. However, when run with Wscript, accumulating the results into a string and using one WScript.Echo statement means that one dialog box is displayed for the entire loop (as shown in Figure 2), rather than one dialog box for each special folder.
Figure 2. For…Next loop builds a string that is displayed in a single dialog box.


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks