Introduction: The Ultimate VBS Tutorial

About: I find a quote from Bill Gates makes a very good description. "Be nice to nerds. Chances are you'll end up working for one."

VBS is one of the most popular programming languages on instructables so there surely will be several instructable guides on programming VBS. However, most of them rush through commands and just show the basic way to do them. They also have a lot of the same basic material. I decided to search all around instructables and until I had a very good list of commands. When I did I started this tutorial that really shows a lot of detail about each command. Now lets start making VBS scripts... 

You will need...

-A computer running Windows 98 or higher (No Macs)
-A brain

Step 1: First Things First...

Open Notepad

Start>All Programs>Accessories>Notepad

Lets get ready to script!

Step 2: Save Me!

Before we begin I should teach you how to save a vbs file in notepad.
To save a file you have to click File>Save As

Then type

Where it says name you can put whatever you want but it MUST end with .vbs

Step 3: Message Boxes

This makes an error message how you like it.

msgbox ("Message goes here",0+16,"Title goes here")

if the user is supposed to make a decision the variable can be added like this.

variable=msgbox ("Message goes here",0+16,"Title goes here")

The numbers in the middle vary what the messge box looks like.
Here is the list

0 - ok button only
1 - ok and cancel
2 - abort, retry and ignore
3 - yes no and cancel
4 - yes and no
5 - retry and cancel
16 - critical message icon
32 - warning icon
48 - warning message
64 - info message
0 = vbDefaultButton1 - First button is default
256 = vbDefaultButton2 - Second button is default
512 = vbDefaultButton3 - Third button is default
768 = vbDefaultButton4 - Fourth button is default
4096 = System modal, alert will be on top of all aplications

Note: There are some extra numbers. You just have to add them to the numbers already there like

msgbox ("Hello World", 0+16+0+4096)

Step 4: Input Boxes

Input boxes allow the user to INPUT a text string. Here is the code for one.

variable=inputbox ("Message","Title","Default Text")

Any of the text between the commas is optional by just not putting anything there like

variable=inputbox ("Type a Message",,"Message")

Step 5: Working With Variables

Math is one of the most simple tasks for a computer. VBS can do complex math in a snap.

Vbs mainly does algebraic math but you can do other than that. First we should make a variable. That is the easiest task in the scripting world.

dim variable name here

Variables can be placed where text goes like this...

Msgbox (var)

The only rule for naming variables is no spaces allowed.
To do math, in between each part of the equation must have a space like so.

y + 5 = 6 * 8

You can also treat an equation like a variable...

Msgbox (5.5 * 3)

Note: VBS will give you an error specifically for dividing by zero so just don't do that.

Step 6: Strings

Strings are variables that hold text. You can make a string like this.

"This is a text string"

VBS will let you treat the text string as a variable but you can make a text string a variable as easy as this.

dim name

name = "This is a text string"

Note:You have to have the quotation marks or else the script will think your talking about a variable.

Note 2:Text strings do have a capacity limit to how much information they hold so be careful.

Step 7: Decisions, Decisions...

What if you needed the program to check if a variable was at a certain value? In programming that is extremely easy. The decision making command is called the IF THEN ELSE statement. An example program would be...

if 200 > 16 then
msgbox ("200 is greater than 16")
msgbox ("200 isn't greater than 16")
end if

That was a foolish example (since 200 will always be greater than 16) of how the if statement works. So now you see IF 200 > 16 THEN let the user know ELSE tell the user it isn't then END the IF statement.

So now you can make a program that will do one of two things. Can you do more? Yes! That is called a nested IF statement. For example...

if 200 > 16 then
msgbox ("200 is greater than 16")
if 200 = 16 then
msgbox ("200 is equal to 16")
msgbox ("200 isn't greater than 16")
end if
end if

This program will say if 200 is equal to, greater than or less than 16.

The same can be used with text strings.

if userinput = password then
msgbox ("Welcome")
msgbox ("Access denied")
end if

Note that 'userinput' and 'password' are variable holding text strings not text strings themselves.

Step 8: Doing the Loop

What if you want a program to repeat something forever or until something happens? Have I got a command for you! It's the do loop command! Its most basic use is this.

msgbox ("Hi")

A more complex version would be

do until 24 > 8
msgbox ("Hi")

A similar version of that code would be

msgbox ("Hi")
loop until 24 > 8

While the two codes look the same there is a difference. That is, the first code won't even go into the loop if the criteria is met before the loop is started while the second one has to go through the loop at least once because the 'loop until' is after everything in the loop.

Step 9: Important Command

One of the things that makes VBS special is it can work with other programs that are compatible with it by creating something called an object. In other words a VBS script can get more commands by controlling other programs. To use objects you have to let the script know what you are going to call it. Here is how you set an object...

set name = wscript.CreateObject("what the object is")

It is a good idea to set objects in the first lines of your code. There are two reasons for that.

1.It makes your program clearer to read.

2.It's impossible to use an object that hasn't been set. Its best to get that done first to avoid errors.

The next two steps will show two examples of objects.

Step 10: Shell

  Say you want your script to do something like run a program or DOS-ish commands. This object is perfect. It is called the Windows Shell object. Here is a simple way to use it to open a directory.

 set wshshell = wscript.CreateObject("") C:\Windows

The C:\Windows folder should open.
Another command is sendkeys. It will, well, send keys, to applications. Say you want to send hello world into a program, just type.

set wshshell = wscript.CreateObject("")
wshshell.sendkeys "Hello World"

Another command is the Echo command which sends a message box with no picture, an OK button, and Windows Script Host in the title. An example is.

set wshshell = wscript.CreateObject("")
wshshell.echo "Hello World"

Step 11: Sapi

Did you ever want to make a computer talk? Sure you could open the text to speech window, but what if you want a computer to read results without any user interface?  Just use the SAPI (Speech Application Programming Interface) object.

Set Sapi = Wscript.CreateObject("SAPI.SpVoice")
Sapi.speak "Hello world"

The speak command can also use variables.

Set Sapi = Wscript.CreateObject("SAPI.SpVoice")
Sapi.speak variable

Step 12: Date and Time

These commands will show the date or time if used correctly. They can be treated like variables.

Date (Returns system date)
Day (Returns the day (1-31)from the date command) ex. Day (Date)
Month (Returns the month (1-12) from the date command) ex. Month (Date)
Year (Returns the year from the date command) ex. Year (Date)
Time (Returns the current system time to the second)
Second (Returns the second (0-59) from the time command) ex. Second (Time)
Minute (Returns the minute (0-59) from the time command) ex. Minute (Time)
Hour (Returns the hour (0-23) from the time command) ex. hour (Time)

For example 

msgbox (Date)
msgbox (Time)

That is a simple program that shows the use of the Date and Time commands. 

A more elaborate program that uses all of the commands listed here and some extra can be found here .

Step 13: Reading Plain Text

Say you want the computer to read from a file and store the contents as a text string. Here is a code that does just that.

Dim Textstring

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objText = objFSO.OpenTextFile("File.txt", 1)

Textstring = objText.ReadAll

Step 14: Final Exam...

This time take what you have learned from this instructable and write a program by yourself. Comment it so we all can enjoy it.

Step 15: Bye

Thank you for reading this instructable. Comment anything cool you come up with so we can all share our discoveries. 

Step 16: Additional Steps: the String Function "Replace"

mohamadelmoussa asked for a command that would replace certain words with others in text strings. The replace command is definitely, what he was looking for. Here is the basic syntax.

Replace(string,"change this to","this")

Here is an example of using the replace function...

text="This is a good instructable!"
msgbox (Replace(text,"a good","an awesome"))

Thanks for the compliment example.