Part 1: Fun with VBS and the WSH

Write your "How To" regarding anything you are comfortable with and you feel it will help the forum members.

NOTE :: All threads started here will appear only after the approval from Administrator
Post Reply
Frederick.
Posts: 7
Joined: Sun Nov 04, 2007 5:23 pm

Part 1: Fun with VBS and the WSH

Post by Frederick. »

Visual Basic Script, or VBS as it is more commonly reffered to, is a very easy to learn scripting language. Here is the first part of a little tutorial that I have put together. All you need is a text editor, I would recomend one called 'Notepad ++' but you can just use Notepad in Windows if you like. In this tutorial I will introduce you to the MsgBox, InputBox and If/Else/End If commands. I hope that you enjoy it!

Script 1
Aim: Make 'Hello World' appear on screen.

Step 1:
Open up notepad and paste in the following test

Code: Select all

MsgBox ("Hello World!")
Step 2:
Save the file as 'script1.vbs and run it.

All done! You should see 'Hello World' written on the screen now.

So... how did that work? Basically, the 'MsgBox' command tells the Windows Script Host, or WSH as I will referr to it by that the text enclosed in brackets and quotes will appear as a text box. So, by changing the text you can make anything appear in the text box.

Now... on to Script 2
Aim: Make a box appear that will ask for your name then reply with a nice message about the pesons name.

Step 1:
Paste the following text into notepad

Code: Select all

Name = InputBox ("What is your name?")
MsgBox ("Hello there "&Name)
Step 2:
Save the file as script2.vbs and run it!

Okay, so it should have replyed saying hello there, then the name that you entered in. Lets see how that worked. For the first line, 'InputBox' is the command that tells the WSH to make a box appear that you can write in. The reason that I put 'Name' there is so that I could referr back to it later. In line two, the text will change depending on whatever is typed in on line one. You can name a variable, (I called mine 'Name'), whatever you want. As long as when you want to enter the text you make sure that it matches up. Please note how I went "Hello there "&Name". You enter the text you want with a space at the end, then ", then &+variable name.

Script 3
Aim: Exactly same as script 2 but with more variables.

I am just going to give you the code for this one. It is very similar to script 2 but with more variables. Edit it yourselves and add more content.

Code: Select all

Name = InputBox ("Hello there, what is your name?")
MsgBox ("WOW! Really? I have always thought that a really good name is "&Name)
Age = InputBox ("What is your age?")
MsgBox ("I remember having fun when I was "&age)
FavThing = InputBox ("What is your favourite thing to do in the weekend?")
MsgBox ("Cool! I love doing "&FavThing)
Script 4
Aim: For a message to appear insulting everyone except the 'master name'

Code: Select all

Name = InputBox ("What is your name?")
If Name = ("Frederick") Then
MsgBox ("You are L33T Frederick!")
Else MsgBox ("You are n00b "&Name) End If
Run it. If you enter in 'Frederick' you will get a nice message, for everything else, you will get something saying you are n00b!

Change the names so that they are nice to you and have a play with the If, and Else commands. I will be talking more about them next time. Please make your own VBS and post them here and I can help you out.


SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

Thank you for sharing the how to.

Your account has been credited with 20 points.
Post Reply