Getting Started with VBScript
As previously mentioned, VBScript is included with Microsoft Windows, so nothing extra needs to be installed to utilise it. VBScript can be written in a simple text editor, such as Notepad, which also comes with Windows, however, using a basic code editor, such as Notepad++, can make it easier, with its code highlighting. The editor being used must allow files to be saved with a ‘.vbs’ extension, which VBScript files must have.
When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in VBScript a line needs to start with a single quote mark as shown below.
' This is a comment in VBScript.
The VBScript code written below simply displays the words ‘Hello World!’. Where this text is displayed depends on how the VBScript is run.
WScript.Echo "Hello World!"
VBScript files can be run in a number of different ways. As VBScript is built into Windows, it can be run simply by double clicking on the file. If a file containing the above code is run in this way, the text ‘Hello World!” will appear in a small dialogue box in the centre of the screen, with an ‘OK’ button to dismiss it.
VBScript files can also be run from the command line, using the ‘cscript’ command. The below example demonstrates this with a file called ‘Demo.vbs’, containing the above code.
It should be noted that, the file path would need to be included if it resides in a directory other than the current one. If the file is run in this way, then the text ‘Hello World!’ will appear in the command window that it is being run from.
A third way that a VBScript can be run is via a batch file. Again, the text would appear in a command window.
If the VBScript being run contains multiple lines of feedback, then it is best to use either the second or third methods of running the file so as not to have multiple dialogue boxes to dismiss.