Friday, July 25, 2008

VBScript part 2: looping and constants

Looping:

For Each...Next:
These commands let you walk through a collection of objects, do something with each of them, then move on to the next one. You cannot perform a WScript.Echo on a collection because each item in the collection can be assumed to have different properties, so which would it echo? Instead you'll need to use a "For Each"..."Next" loop.

For...Next:
"For...Next" differs from "For Each...Next" in that you need to know how many items there are in the collection in order to use "For...Next" while you do not when using "For Each...Next".

Do While...Loop:
This command runs a script as long as a certain condition is in effect.

Wscript.sleep:
Wscript.sleep pauses the script for the specified period of time (in milliseconds). An example of a one second pause would look like:
WScript.Sleep 1000
...but don't rely on it for scientific measuring of time under a second, it's just not that accurate!

Timer:
The timer function is used to time how long it takes to run a script. You use it once, it counts the number of seconds since midnight, then you use it again at the end and when you subtract the first from the second, you get the length of time that it took to run the script as your result. The variable names that are often used for this are "startTime" and "snapTime".

Constants:

"Const": You define a constant by using "Const" (variable) = (value).
Example:
Const DriveType = 3


General Info:

Interacting with programs/services that are already running:
...if the program you're using is already running, like WMI for "GetObject" you do not have to create an instance of the WMI object by using "CreateObject". Instead, you can just get it by using "GetObject".

Line breaks:
An underscore at the end of a line breaks the code into two lines, usually to make it easier to read. When a line that's inside parenthesis is broken up an ampersand ("&") is needed as well.

WMI:
To get information about drives you need to connect to WMI using GetObject("winmgmts:") then you can use a query to select the drives you want.

Time Stamp:
Wscript.Echo Now
Will print out current date and time. Useful for logs. Also can be used to time your script since it reports the time down to the second.

Space ():
The "Space ()" command is built into the language and, therefore, does not need to be defined or declared. It acts like a variable tab command, so you just need to tell it how many spaces you want and it'll skip that many spaces.

vbNewLine:
The vbNewLine command is also built-in, it tells the script to print out a new line.

Thursday, July 24, 2008

Learning VBScript: structure and basic commands

Script Headers:
This is how you start your script; by telling the computer what to expect and how to run the script.

"Option Explicit": You're going to list each variable in the script before it is used. If you don't use this it'll be assumed that any statement that VBScript doesn't recognize is a variable. In order for it to work, though, it must be the first non-commented out line in the script. Option Explicit can also be thought of as a spell checker since it'll give an error if you misspell a variable name later in your code.

"Dim": Declares a variable. (Example: "Dim regComputerName")

"On Error Resume Next": Tells the computer that if it runs into an error it should keep going and try the next line of the script. (You may want to omit this -at least until the code has been tested as it may hide mistakes you'll want to know about.)

Reference Information:
This is the second section in your code; where you assign values to your variables, etc.

Example: (using "regComputerName" as the variable)
regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control" &_
"\ComputerName\ComputerName\ComputerName"

HKLM:
The script knows that HKLM stands for HKEY_LOCAL_MACHINE, but be aware that it's case sensitive! Also, the &_ is just for continuing the code on a new line.

Avoid typos in registry keys:
To make sure you have the correct registry key, in regedit.exe you can go to the "Edit" menu and choose "Copy Key Name" to copy it rather than trying to type it and risk typos ;-).

Worker Information:
In this section your variables, etc are actually put to work doing something!

"Set": Command used to assign an
object reference to a variable. Example:
Set objShell = CreateObject("WScript.Shell")

"Create Object": As used in the example above, this part of an expression assigns a variable name to a reference.

"WScript.CreateObject": 99% of the time this is the same as using "Create Object"

Output:
How you display information.

"WScript.Echo": used to display text in a command window or pop up box depending on how it's run. When run using CScript, it'll write it in a command shell. When you use Wscript.exe it'll write to a Windows dialog box.
Example:
WScript.Echo ComputerName & " is computer name"

Symbol "&": The "&" symbol just puts two things together. Here those two things are the value of "Computer Name
and the text "is computer name".

General Info:

Comments:
Preface comments with a single quote " ' ".