Many people only experience VBScript when using a web site - and then only if the web browser is Internet Explorer. However, VBScript is a powerful scripting language that can be used at anytime on a windows pc - and it can even be used as a command line application.
Running a VBScript Program with Windows Explorer
An easy way to run any VBScript file is to:
- open Windows Explorer
- move to the directory containing the script file
- double click on the file
The file itself will need a .vbs extension and can contain something like:
The script will display a message box with the words "This is a VBScript Program", however this script can be run as easily from the command line.
Running a VBScript Program from a DOS Prompt
A Windows user can run a VBScript program from a DOS prompt by:
- opening the MS-DOS prompt (or C:\Windows\command.com) from the Windows menu bar
- moving to the directory containing the vbs file
And then typing:
And again the message box will be displayed
Turning Off the Microsoft Header
Every time that a VBScript file is run from the command line it will display a Microsoft header:
However, the user can turn the header of by using the nologo option:
Now any output will clean and uncluttered.
Outputting to the DOS Screen
The programmer can instruct the script to send its output to the DOS screen by working with the scripting host's standard output:
A single line will now be written to the screen:
And, of course, functions can be added to the script:
Now the output will be:
And, as any developer would expect, the script can take inputs from the DOS prompt as well
Obtaining Inputs from the DOS Prompts
As well as sending outputs to the DOS screen VBScript can accept inputs from the command line - by intercepting the scripting host's standard input:
It's worth noting from this that VBScript has two write modes:
- write - writes some text to the screen
- writeline - writes some text to the screen and ends it with a new line
The result is something like:
And with that the programmer has full control of the inputs and outputs of a VBScript application.
Summary
A VBScript application can be run from the DOS command line by using cscript (with /nologo to remove the Microsoft header). The script can then work with the script host's standard input to obtain information and the standard output to display any results - turning VBScript into a command line application.