My .vbs notes on a (web) page:

Intro

Script Hosts

Special Characters

Formating Stuff

Wscript commands

Variables etc

If

Select Case

Loops

String Manipulations

Dates

MISC

 

Intro:

vbs = Microsoft Visual Basic Script

Script hosts:

Sesame Script: Starting and Stopping Scripts

Special Characters:

Sesame Script: Special Characters

Ex:

' a comment is any line that starts with a single quote character "'"; you can also put a comment at the end of a line

Ex:

Wscript.Echo First & " " & Last ' primary purpose of the ampersand is to concatenate

Ex:

Wscript.Echo "There are " & intComputers & _
   " computers in use by the Accounting department: " & _
   intServer & " servers and " & intDesktop & " desktop    machines."
   ' The line continuation character is the underscore "_"

Ex:

Wscript.Echo "Line 1" : Wscript.Echo "Line 2" ' a way to    put more than one command on a line
 

Formating Stuff:

Ex:

vbtab ' places a tab into output stream

Ex:

vbcrlf ' places a carrage return / line feed into output stream

 

Wscript commands:

Ex:

Wscript.Echo "line 1"

Ex:

Wscript.Quit ' stops script from within

Ex:

Wscript.StdOut.Write "ABCD" ' Outputs "ABCD" and the next output will follow the "D"

Ex:

Wscript.StdOut.WriteLine "WXYZ" ' Outputs "WXYZ" and a carriage return

Ex:

Wscript.StdOut.WriteBlankLines 2 ' Outputs two carriage returns

 

Variables etc. (whole bunch of vbs architecture/theory stuff):

Sesame Script: Class is in Session

Sesame Script: Variables and Constants

Data Types:

Ex:

Const X = 2 ' put a literal value into a constant, another convention is to use caps for consts

Ex:

Dim arrArray(7)
' Mostly required to dim arrays as otherwise the syntax looks like a method
' Tells that you’ll be using this as a variable in the script
' Here seven is the size of the array (eight elements, 0 though 7)

Ex:

ReDim arrArray(5) ' Use after a Dim arrArray() used earlier in a script where the size was not yet known, or want to change

Ex:

Option Explicit ' All variables MUST be dimmed after this command is used, good best practice

Ex:

arrArray(3) = "m" ' Assign a value to an array element

Collections:

Array:

Class:

Properties:

"Properties describe something, whereas methods take an action on that thing."

Property is to Method as Feature is to Action.

Methods:

Method Parameters:

Binding:

Creating an Object Reference – Microsoft Windows 2000 Scripting Guide

Binding – Microsoft Windows 2000 Scripting Guide

Connecting to Objects – Microsoft Windows 2000 Scripting Guide

 

 

If... Then... ElseIf... Else... End If:

Sesame Script: Make a Decision

Ex:

If condition Then
action
End If

Ex:

If condition Then
action1
action2
End If

Ex:

If condition Then
action1
action2
Else
action3
action4
End If

Ex:

If condition1 Then
action1
ElseIf condition2 Then
action2
Else ' else is a catch-all if all the above ifs evaluate false
action3
End If

If…Then…Else Statement – VBScript Language Reference

Taking Multiple Actions by Using If Then Else – Microsoft Windows 2000 Scripting Guide

Ex:

If condition Then Action ' this is the un-prefered syntax, all on one line with no End If

Select Case:

The Select Case conditional statement selectively executes different groups of code by comparing a variable to a Case (a series of conditions). If one of the cases (conditions) is satisfied, then the code associated with that case is executed.

Choice = 1 ' could be a string, etc.
Select Case Choice
   Case 1
      ' Case 1 actions

   Case 2
      ' Case 2 actions
   Case 3, 4
      ' Note that two or more comma seperated cases can exist on one line like an Or
      ' DevGuru example of this
      ' Case 3 actions
   Case Else
      ' Case not 1, 2, 3, or 4 actions
      ' The Else is optional, but must be at the end of the Select block

End Select
  

Loops:

Sesame Script: Thrown for a Loop

For Each... Next loop cycles once for each element in an object (collection or array)

Ex. For Each... Next example with a Collection:

Set Drives = CreateObject(Scripting.FileSystemObject.Drives)
For Each Drive in Drives ' Note the plural and singular tenses!
Wscript.Echo "Drive letter: " & Drive.DriveLetter
Next
Ex. For Each... Next example with an Array:
Dim arrArray(3)
arrArray(0) = 100
arrArray(1) = 150
arrArray(2) = 200
arrArray(3) = 250
For Each intArray in arrArray
Wscript.Echo intArray
Next

For... Next loop cycles based on a counter

Ex. For... Next structure:

For <counter> = <start> to <end> [Step <intStep>]
' do something
Next

 

Ex. For... Next loop:

Dim arrCount()
size = 3
ReDim arrCount(size)
For i = size to 0 Step -1
    arrCount(i) = i
    Wscript.Echo arrCount(i)
Next
 

Ex. Do... Loop Until loops:

Do
   ' actions
Loop Until bStop

Do
   ' action1
   ' action2
Loop Until lcase(strChoice) = "exit"

Note that the Loop Until is not evaluated until the code goes through the Do/Loop Until code block and hits it

 

 

String Manipulations:

Ex. of RIGHT:

NewString = Right(SourceString, intNumberOfCharactorsFromRightToReturn)

Ex. of LEFT:

NewString = Left(SourceString, intNumberOfCharactorsFromLeftToReturn) 

Ex. of MIDDLE:

NewString = Mid(SourceString, intPositionInStringToReturnFrom) ' Returns from that spot to the end of the string

Ex. of MIDDLE:

NewString = Mid(SourceString, intPositionInStringToReturnFrom, intNumberOfCharactorsFromMiddleToReturn) 

Ex. of LENGHT:

intLenghtOfString = Len(SourceString)

Ex. of LCase:

sLowerCase = LCase(sMixedCase)

Dates:

Ex:

Wscript.Echo "Now: " &                       Now
Wscript.Echo "Date: " &                      Date
Wscript.Echo "Two months from Now: " &       DateAdd("m", 2, Now)
Wscript.Echo "Days left in the year: " &     DateDiff("d", Now, #1/1/2008#)
Wscript.Echo "Day: " &                       Day(Now)
Wscript.Echo "Month: " &                     Month(Now)
Wscript.Echo "Year: " &                      Year(Now)
Wscript.Echo "Day of the week: " &           Weekday(Now)
Wscript.Echo "Day of the week, readable: " & WeekdayName(Weekday(Now))
 

Stuff that goes in the "what" part of DateAdd and DateDiff:

 

MISC:

Ex:

now ' returns the date and time