VBScript Beginning Programming
VBScript Beginning Programming
Seminar
Beginning Programming
With
Visual Basic Script Edition
Tom Burt
August 31, 2006
http://www.scscc.com/smnr/VBScript_Beginning_Programming.pdf
http://www.scscc.com/smnr/Programming_Examples.zip
Seminar Agenda
Well spend the first half of the seminar covering the key programming concepts of
the VBScript language. VBScript can be used in stand-alone programs, but it also
very commonly used in web-site development and in web pages with dynamic
content.
Most of these concepts are common to all modern programming languages, though
syntax and library functions will differ.
Other popular scripting languages include:
Javascript / Jscript, PERL, PYTHON, PHP
Popular program (EXE) development languages include:
C, C++, Java, VB 6, VB.Net, C#.Net
Complete Eclipse Java Developments can be downloaded free from:
Sun Microsystems and from IBM.
http://www.microsoft.com/technet/scriptcenter/default.mspx
The slide above lists the topical areas we will cover before turning to the two
sample programs.
Programming Cycle
Design
Program
Code Program
Statements
Save Program
Statements
Run Saved Program
Note Errors
When the scripting runtime is installed, it will register as the handler for .VBS
files.
To run a script just double click on the saved .VBS file.
Often, to help follow the script programs flow and to see what values variables
currently have, it is helpful to embed temporary Msgbox statements. Msgbox can
display a string and will pause for a user OK before continuing.
In-line calculation
Dim fWidth, fHeight, fDiagonal
fHeight = 3.0
fWidth = 4.0
fDiagonal = Sqr( (fHeight * fHeight ) + (fWidth * fWidth ) )
Msgbox fDiagonal = & fDiagonal
===========================================================
Calculate diagonal using Pythagorean theorem
Function CalcDiagonal (fWidth, fHeight)
CalcDiagonal = Sqr((fHeight * fHeight ) + (fWidth * fWidth ) )
End Function
Msgbox CalcDiagonal = & CalcDiagonal
===========================================================
Arrays
Dim Values(20)
Values(0)=1.5
Values(1) = 3.0
VBScript Constants
Numbers
#01/01/06#
#07-04-2006 1:35 PM#
Strings
Happy Days
Sad days
Booleans
True
False
Intro - Programming VBScript
VBScript Operators
Math:
+, -, *, /, mod
String concatenation:
&
Boolean:
Operand Grouping
()
10
Variable = Constant
Variable = Expression
Variable = function result
Examples:
i = 20
Last_Name = Burt
Pi = 3.1415927
Due_Date = cur_Date+30
circumference = 2 * pi * radius
Name = First_Name & & Last_Name
iDays = abs(cur_date file_date)
i = instr(File_Name, .)
l = len(Last_Name)
avg = (a + b + c) / 3
11
Do Until <condition>
Goto <label>
12
Block of statements
Block of statements
[Else]
Block of statements
[End If]
A single statement block on the same
line as the IF need not have a closing
Endif.
13
VBScript Loops
For i = 1 to n step 1
Loop
Do Until <condition>
Loop
Loops are used to repeat execution of a block of statements until some condition is met.
Usually loops operate on a sequence of data elements, such as an array or collection.
In the For loop, the loop variable (e.g. i) is incremented by a specified step until the loop limit is
reached.
For i = 1 to 15
A(i) = 3 * i + 15
If i > 10 Then Exit For
Next
Do While and Do Until have more flexible Conditions
KeepRunning = True
Do While KeepingRunning
<statements>
If <some condition> Then Exit Do
If <some other condition> Then KeepRunning = False
Loop
14
Block of statements
Block of statements
Case Else
Block of statements
15
Bio-Break
10 Minutes
16
17
VBScript has a large built in library of functions that you can use in writing
programs.
It is worth exploring this library, since the built in functions run much faster than
functions written in interpreted VBScript.
The string functions are particularly important, since most scripts will do extensive
string manipulation.
18
Sub CreateFile()
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\MyFiles\testfile.txt", True)
' Write a line with a newline character.
tf.WriteLine("Testing 1, 2, 3.")
' Write three newline characters to the file.
tf.WriteBlankLines(3)
' Write a line.
tf.WriteLine("This is a test.")
tf.Close
End Sub
Sub ReadFiles
Dim fso, f1, ts, s1, s2
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
' Read the contents of the file.
Set ts = fso.OpenTextFile("c:\MyFiles\testfile.txt", ForReading)
s1 = ts.ReadLine
s2 = ts.ReadLine
ts.Close
End Sub
19
VBScript Example 1
General Meeting Number Drawing Tool
Input the starting and ending ticket numbers
Use random number generator:
20
VBScript Example 2
Clean Out the Temp Folder
Delete any files not created today
General Steps
21
http://www.csidata.com/custserv/onlinehelp/VBSdocs/VBSTOC.htm
http://msdn.microsoft.com/vstudio/express/vb/
The above hyperlinks go to the major home networking equipment vendors web
sites. All these vendors provide a wealth of technical information.
There is also a link to the Microsoft home networking site, which again has much
useful information.
Finally, the Windows XP Help system has extensive information on networking
which is worth reading for more information.
22
23