fredag den 11. september 2009

VB-Script

Now to another easy way to script your computer - throug vbscript. VB stands for Visual Basic, and perhaps you have heard about it.

Again I will start with a very easy script.
1. Make a file - the same way as in the first blog, but this time it should be renamed to HelloWorld.VBS
2. Edit the file and add these lines

Option Explicit
Wscript.echo "Hello World!"
msgbox "Hello world!"

3. Save the file and doubleclick.

You should see two dialog boxes on your screen with "Hello World!" and you might wonder why I showed you both methods, but it has a very good reason.

Try the following:
1. Click StartRun and write "wscript \helloworld.vbs" where path is the path to the file.. (TIP: You can drag the file to the textfield) - the same thing happens.
2. Click StartRun and write "cscript \helloworld.vbs" - notice what happens.

The wscript.echo checks if its running in a commandline or a windows environment and writes the text in the right environment. Msgbox allways uses a dialog. If you dont have a chance for user interaction, you do not want a dialog box that stops the script.

I haven't mentioned the first line (Option Explicit) yet. Actually it's not important in this example, but I personally thinks it's a good thing to learn from the start. When you put Option Explicit in the start of a vb-script, the interpreter demands that all variables are declared. If you don't understand this right now - don't worry, i'll get back to it.

Ingen kommentarer:

Send en kommentar