|
|
|
Windows Scripting: Command Line VBScriptHow to Use VB Script Files from the Windows DOS Prompt
VBScript is not just a programming language for Intranet web sites; it is also a powerful scripting language - a scriting language that can be used from the command line
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 ExplorerAn easy way to run any VBScript file is to:
The file itself will need a .vbs extension and can contain something like: option explicit
dim text
text = "This is a VBScript Program"
msgbox text
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 PromptA Windows user can run a VBScript program from a DOS prompt by:
And then typing: cscript dos_command.vbs
And again the message box will be displayed Turning Off the Microsoft HeaderEvery time that a VBScript file is run from the command line it will display a Microsoft header: Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
However, the user can turn the header of by using the nologo option: cscript /nologo dos_command.vbs
Now any output will clean and uncluttered. Outputting to the DOS ScreenThe programmer can instruct the script to send its output to the DOS screen by working with the scripting host's standard output: option explicit
dim text
dim output
set output = wscript.stdout
text = "This is a VBScript Program"
output.writeline text
A single line will now be written to the screen: > cscript /nologo dos_command.vbs
This is a VBScript Program
And, of course, functions can be added to the script: function pi
pi = 3.14159265
end function
output.writeline "Pi is " & pi
Now the output will be: Pi is 3.14159265
And, as any developer would expect, the script can take inputs from the DOS prompt as well Obtaining Inputs from the DOS PromptsAs well as sending outputs to the DOS screen VBScript can accept inputs from the command line - by intercepting the scripting host's standard input: dim input
dim input_line
dim circumference
dim area
set input = wscript.stdin
output.write "Enter a circle radius> "
input_line = input.readline
circumference = 2 * pi * input_line
area = pi * input_line * input_line
output.writeline "Circumference is " & circumference
output.writeline "Area is " & area
It's worth noting from this that VBScript has two write modes:
The result is something like: Enter a circle radius> 1
Circumference is 6.2831853
Area is 3.14159265
And with that the programmer has full control of the inputs and outputs of a VBScript application. SummaryA 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.
The copyright of the article Windows Scripting: Command Line VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish Windows Scripting: Command Line VBScript in print or online must be granted by the author in writing.
Comments
May 26, 2009 1:51 PM
Guest
:
1 Comment:
|
|
|
|
|
|
|
|