Brief Intro to VB Scripting
Posted by Clarke M on 03 April 2006 08:24 PM
Tired of taking lip from your computer?
..'Bad command or file name' ..'illegal operation' etc etc
Ever wanted to turn the tables and order your computer around for a change?
It may not be as difficult as you think.
Some clarification before we get started
If you want a career in computer programming this is not for you.
We are not going to work at the level of Zeros and Ones here,
But if you just wish to experiment a bit with writing a script for your computer to follow, read on.

Required Tool.
You need a plain text editor. That is some software that will allow you to create and save a file without any fancy formatting. No special fonts, bold or italics. Just plain vanilla A-Z, a-z and 0 - 9. Any word processor can do this but the best way is to use one that can't do anything else.

Don't worry I'm sure that you already have one.
Have a look under 'Programs' - 'Accessories' for something called 'Notepad'.
If you can't find it try this..
Click 'Run' and type in the word 'notepad'
When note pad opens type in the following.
msgbox("Testing 1..2..3..")

Save the file as test.vbs. Put it somewhere you can find it easily.
Be carefull to set the 'Save as type' to 'all files' or notepad may save it as test.vbs.txt

Now to test your creation
This will also be a test of your antivirus protection.
Locate your file and double click it.
If you have good security software it should spring into action and question whether this script is to be trusted. This is because scripts can be written to do almost anything on your computer and many a virus has been written as a script.
Ask your security software to allow this script to run.
You should see the following Message Box appear on your screen. thats not very exciting Lets move on
You have used a Message Box, now lets introduce an Input Box.
Open test.vbs in Notepad and replace the contents with the following 3 lines.

Dim Input
Input = InputBox("Enter your name")
MsgBox ("You entered: " & Input)

OK, go ahead and test it
OK, now what does it all mean?
The first line reserves a chunk of memory for storing your stuff.
This is similar to using the memory key on a calculator but since you may wish to use more than one storage area it needs to be given a name.
The 'Dim' statement dimensions this and gives it a name. We called it 'Input' but you could have called it XYZ or anything you wanted.
Well, almost anything. Some words are 'Reserved' because they are scripting commands and would confuse your computer. Not hard to do as you probably know icon_sad.gif .
The second line takes your memory chunk and stores the result of an input box in it. Now you can use 'input' in your message box and it will display its memory contents. Please note that there are no quotation marks around 'input' when it is used in this way.
Why? Well try it and see.....

See why?.... It gave you the actual text of the name of your memory space instead of the contents.

Here is a simple game script for you
Copy and paste the text between the '''''''''''''''''''''''''''''''''' lines.


' This script is a simple guess-the-number game that will introduce you to some more scripting concepts.
' Please Note: Any line that starts with a single quote ['] is ignored by the script processor
' so we can add comments to our script without any risk of confusing the computer.

'first we will dimension the memory spaces (officially called "variables")
dim upperbound, lowerbound, chosen_num, best_guess, still_guessing, guess_number

' now we give the variables their initial values

' the aim here is to have the computer pick a number between 1 and 100
' The next command ensures that it will not always pick the same number.
' we store the chosen number in a memoryspace (variable) called chosen_num
chosen_num = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

'to cheat just remove the single quote from the line below.
' msgbox("The number is " & chosen_num & ". CHEATER!!!")

' the 'while' statement below causes the script to repeat all statements between the 'while' and the 'wend'
'until still_guessing is found to be false.

while still_guessing
' gather input from player using an input box
' Note the inputbox is wrapped in another statement "int()" this ensures that
' best_guess ends up with an integer. a whole number with no fractions or decimals.
best_guess = int(InputBox("Enter your guess"))

' Add one to the number of guesses each time this part of the script is repeated.
guess_number = guess_number + 1

' this "if" compares the players guess with the number the computer chose.
if best_guess=chosen_num then ' the guess was correct

' this "if" inside of an "if" is optional. Its only purpose it to make the message
' grammatically correct in the unlikely event of the first guess being right.
if guess_number = 1 then
msgbox("You got it in " & guess_number & " guess! the number is " & chosen_num)
msgbox("You got it in " & guess_number & " guesses! the number is " & chosen_num)
end if
still_guessing = false
elseif best_guess > chosen_num then ' the guess was too high
elseif best_guess < chosen_num then ' the guess was too low
End if
' don't forget to tell the script where your "if" ends
' or you may have unexpected results.

' the "wend" statement marks the end of the "while" loop


So, thats it.. there is your introduction to scripting.
You were using a language called Visual Basic, Scripting edition and your scripts were run in Microsoft Windows. The part of Windows that makes the script commands work is called 'Windows Script Host'.

For more information on creating scripts visit Microsofts Web site:

To download the scripting manual:
(588 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Help Desk Software by Kayako Fusion