WSH in Other Languages - VBScript
(Page 2 of 5 )
Microsoft Visual Basic Scripting Edition (VBScript) is an Active Scripting language developed by Microsoft in 1996 with the release of Internet Explorer 3.0. The syntax is a subset of Microsoft’s Visual Basic programming language.
VBScript is the most commonly used language in WSH because it is easy to learn and well-integrated throughout the Microsoft family of software. Its real-world command set and non-strict nature allow novice users to begin writing code very quickly.
The VBScript engine comes integrated into the Windows Script Host environment. Simply naming a plain text file with a .vbs extension is all that is required to make the script executable.
Accessing COM Objects
As an object-oriented environment, WSH relies heavily on its ability to use COM objects installed on the system. While the most commonly used objects are OLE automation objects, WSH (and VBScript) will allow you to connect to and use any COM-enabled library that has a scripting interface.
Set objFso = CreateObject("Scripting.FileSystemObject")
In order to use objects and access their various properties, methods, and events you must first create an instance of the object in your script. This is most commonly done using VBScript’s CreateObject method.
Because VBScript is fully integrated into the WSH environment, intrinsic objects such as WSH’s WScript object model are readily available and do not require initialization.
VBScript also provides a GetObject method that returns a reference to an object instance that is already running.
Set objFolder = objFso.GetFolder("C:temp")
The properties and methods of an object are accessed using standard object-dot syntax. In this case, the FileSystemObject’s GetFolder method is being used to return a Folder object that represents a supplied system path.
Set colFiles = objFolder.Files
Finally, the Folder object’s Files property is used to return a collection object containing object references for each of the files in the specified folder.
Working with Collections
Various scripting languages handle collection objects differently. Since collections are quite commonplace in the WSH environment, I’m including a section that covers this topic for each of the languages I present today.
For Each objFile In colFiles
WScript.Echo objFile.Name
Next
In VBScript, collections may be accessed directly. It provides a For Each…Next structure for iterating through each object in the collection. Here, I’m quite simply just printing the file name back to the screen.
Additional Notes About VBScript
VBScripts provides a very relaxed programming environment. VBScript is not case-sensitive, nor does it require any line endings. It also ignores extraneous whitespaces; however, statements may not span more than one line without using the special underscore character. Finally, variables are all of type variant and may be explicitly declared, but this is not required.
Next: Jscript >>
More Windows Scripting Articles
More By Nilpo