BTW - See an example here of the sort of
things I did whilst others my age were out having fun -
HERE - This is taken from a popular weekly magazine
of the time "Home Computing Weekly"
I find VBScript very useful both at home
(it controls my
house via my home automation system) and at work
(automating
tasks, gathering audit info from computers etc.), you can do
some very clever things with it very easily - this page is aimed at
getting you started as I found it difficult to find any decent info on the
basics. Once you get the feel for how it works then it is easy to
find examples for what you wish to do via Google etc., so once you have
read through this page there will be no stopping you :-)
Getting
started
First of all, just to show how easy it is to create a script and to get an
idea of what it is all about we shall
(in the time honoured way)
create a script to display "hello world"
1 - Open Notepad on your computer
(click start
then run and enter Notepad)
2 - Enter the following line
msgbox
"hello world"
3 - Save the file as
test.vbs
Now browse to this file you have just saved
(in my computer) and double click on it
- It is that simple, you are now ready to start experimenting with all
the VBScript commands to see what you can do
note
- If when you double click on the file it just opens up in notepad
again, this will be that your computer is set not to show file
extensions (i.e. it has saved the file as test.vbs.txt
) - Google "windows show file extensions"
Try replacing the command in your script with - msgbox "The date is " & date
You could create a simple script to
check if it is an important date
If left(date,5) = "09/11" Then
'left(date,5)
just uses the first 5 characters of todays date
MsgBox "Happy
Birthday"
End If
Put this into the Startup folder on your PC and there, you have
already created something useful with VBScript :-)
Getting
a
bit more advanced
Here is a simple script to copy a file "test.txt" from c:\ to c:\temp
Set
Fso = WScript.CreateObject("Scripting.FileSystemObject") 'this line is just a standard
VBScript command to invoke the "File System" addin (dont
worry if you don't understand it)
FSO.CopyFile "c:\test.txt" , "c:\temp\" ,
True
Now if you only wanted the file to copy after a certain date you could do
the following:
Set Fso =
WScript.CreateObject("Scripting.FileSystemObject")
If
date > "08/09/2009" Then
FSO.CopyFile
"c:\test.txt"
, "c:\temp\" , True
End If
Now you can add the following so it will tell you if the file has been
copied
Set Fso =
WScript.CreateObject("Scripting.FileSystemObject")
If
date > "08/09/2009" Then
FSO.CopyFile "c:\test.txt" ,
"c:\temp\" , True
MsgBox
"File copied"
Else
MsgBox "File not copied"
End If
Now if the file to be copied doesn't exist you will get a meaningless
error message, so you can test for this before attempting to copy the file
Set
Fso = WScript.CreateObject("Scripting.FileSystemObject")
If date > "08/09/2009" Then
If
FSO.FileExists ("c:\test.txt") = True Then
FSO.CopyFile
"c:\test.txt" , "c:\temp\" , True
MsgBox
"File copied"
Else
MsgBox
"Error - c:\test.txt does not exist"
End
If
Else
MsgBox
"File not copied"
End If
Now you may not want it to overwrite the destination file if it already
exists
Set Fso =
WScript.CreateObject("Scripting.FileSystemObject")
If date > "08/09/2009" Then
If
FSO.FileExists ("c:\test.txt") = True Then
If FSO.FileExists ("c:\temp\test.txt")
= True Then
Msgbox
"Error - destination file alread exists"
Else
FSO.CopyFile
"c:\test.txt" , "c:\temp\" , True
MsgBox
"File copied"
End If
Else
MsgBox
"Error - c:\test.txt does not exist"
End
If
Else
MsgBox
"File not copied"
End
If
So you get the idea how you can quickly and easily use VBScript to do
exactly what you require
Example
Game
Here is a very simple game using VBScript
you try to guess a number between 1 and 100 - There is no error trapping
etc. in this so there is plenty of room for improvement here but it
demonstrates the sort of thing you can do
It uses variables which we have not covered yet, this just assigns a word
or number to a name
e.g. you could say FRED=10 then work out what FRED plus 5 is
dim FRED
'This
declares you will be using a variable called FRED
FRED = 10
'This
assigns the number 10 to FRED
MsgBox FRED +
5 'This displays what
FRED plus 5 is. i.e. it displays "15"
Note: The variables used in this game are GNUM=The random number
you have to guess, GUESS=Users crrent guess and COUNT=the number of
guesses so far
randomize
'This stops the same numbers coming up
every time you start the program
dim GNUM, GUESS,
COUNTt
'Declare variables used in the script
gnum=int(rnd()*100)+1
'pick a random number between 1 and 100
count=0
'set number of guesses counter to 0
Do
'keep repeating this section until the guess is
correct
count
= count +1
'increment the guess counter by 1
guess=inputbox("Enter
guess number " & count)
If
gnum = cdbl(guess) Then Exit do 'Stop
when correct (cdbl converts text format to number format)
If
cdbl(guess) > gnum then 'Display
if the guess is too low or too high
msgbox
"lower"
Else
msgbox
"higher"
End
If
Loop
'Jump back up to the Do command
Msgbox "Correct in " & count
& " tries"
Now you have a feel for the basics you will be able to search for other
peoples scripts on Google and see how to do things.
There are plenty of forums and guides out there, it is just the very
basics
(as covered above) which I found was difficult to
figure out and so it was difficult to get started.....
Before spending too much time learning VBScript I would recommend you have
a look at the open source scripting language
Autohotkey
I think it is more powerful than VBScript and you can do some really
clever things with it very easily.
If your interest is more in creating graphics or web based stuff then
Javascript will be much more of interest, take a look at
THIS,
I have been amazed at how easy this type of thing is tto produce these
days...
BTW - If you enjoy this sort of thing you may be interested in having a
look at the Arduino? Have a look
HERE
Let me know if this page has been
useful, if I get enough interest I will continue to add to it -
alanesq@disroot.org
Links
Beginners
Guide
VBScript Commands
Some
examples of VBScript in web pages