One of the little known secrets about Microsoft Windows is that it has its own built in scripting language - and it's Basic - the same simple yet powerful language that forms the backbone of that world famous program Microsoft Visual Basic.
The key differences between Visual Basic programming and Windows scripting are that:
- Visual Basic is an event driven language (click that button, select from that menu, etc) whereas Basic is a procedural language ( do ... until, if ... then ... else ... end if)
- Visual Basic has its own dedicated IDE (Integrated Design Environment) whereas Basic just needs a plain old text editor such as notepad, although editors which display coloured syntax highlighting (such as Notepad++ or UltraEdit) are nice (but not essential)
- Visual Basic needs to be bought and installed whereas Basic comes automatically (and freely) with every Windows PC
- Any Visual Basic program must be compiled on a PC before it can be run, but a Basic script simply uses an interpreter that is on every Windows PC
And it's those differences that makes Basic so powerful and versatile.
Writing a Basic Script for Windows
It's very easy to create a Basic script - a script that will be quite familiar to any Visual Basic programmer (or to any other programmer, for that matter) - it just requires the programmer to open up a text editor and then start scripting extremely powerful applications - for example one that will create a Microsoft Word document and then write to it:
option explicit 'enforce the definition of variables
dim word, doc, ip
set word = createobject("word.application")
word.documents.add
set doc = word.activedocument
doc.select
ip=True
while ip <> False
ip = inputbox ("Please enter some text")
if ip <> False then
doc.range.insertafter ip & vbCr 'vbCr is a Basic constant representing a carriage return
end if
wend
doc.saveas ("C:/test.doc")
word.visible = True
The code is very simple, but very effective:
- it creates an object -the Word application itself
- it then opens a new Word document and selects it
- next it loops - asking the user for input and writing the response to the document
- finally (when the user has finished entering details) it saves the file and then displays the result (the completed Word document) on the screen
One interesting point this reveals is that all of the activities are hidden in the background unless the code specifically sets the documents visible property.
There is, of course, an alternative ending to this script; instead of:
word.visible = True
the developer could write:
word.quit
set word = nothing
This would simply close the file (and reclaim any memory that may have been used).
Running a Basic Script in Windows
The script can be run in one of two ways:
- if the file is saved with a .vbs extension (e.g word.vbs) then the script can be run by double clicking on it from the Windows Explorer
- the file can be run from the command line by using the cscript command, for example cscript word.vbs
If the script is run from the command line then some additional text is always displayed as a header, but this can be turned off by using the nologo key word:
cscript //nologo count.vbs
Conclusion
Windows scripting:
- is not complicated
- it needs no additional software installing
- completely free - just waiting to be used.
It is also:
- very powerful
- very versatile
- completely under utilised
and if that's not enough - it's on every Windows PC in the world.