Suite101

Windows Scripting: Command Line VBScript

How to Use VB Script Files from the Windows DOS Prompt

© Mark Alexander Bain

Dec 9, 2008
A VBScript vbs File, Mark Alexander Bain
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 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:

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 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:

cscript dos_command.vbs

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:

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 Screen

The 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 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:

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:

  • 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:

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.

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.


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.


A VBScript vbs File, Mark Alexander Bain
Command Line VBScript, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
May 26, 2009 1:51 PM
Guest :
good
1 Comment: