Object.Method(Argument1, Argument2, ...)
WshShell.RegWrite strName, anyValue[, strType]
WshShell.RegWrite "HKCU\Software\Microsoft\Windows Script Host\Test", "Foo", "REG_SZ"
Assigning an Object to a Variable
If you’re using JavaScript, you assign an object to a variable using a standard variable assignment:
var variableName = ObjectName
variableName | The name of the variable |
ObjectName | The object you want to assign to the variable |
In VBScript, you assign an object to a variable by using the Set statement. Set has the following syntax:
Set variableName = ObjectName
variableName | The name of the variable |
ObjectName | The object you want to assign to the variable |
You’ll see later on
that you must often use Automation to access external objects. For
example, if you want to work with files and folders in your script, you
must access the scripting engine object named FileSystemObject. To get this access, you use the CreateObject method and store the resulting object in a variable, like so:
Set fs = CreateObject("Scripting.FileSystemObject")
Working with Object Collections
A collection is a set of similar objects. For example, WScript.Arguments
is the set of all the arguments specified on the script’s command line.
Collections are objects, too, so they have their own properties and
methods, and you can use these properties and methods to manipulate one
or more objects in the collection.
The members of a collection are elements. You can refer to individual elements by using an index. For example, the following statement refers to the first command-line argument (collection indexes always begin at 0):
If you don’t specify an element, the Windows Script Host assumes that you want to work with the entire collection.
VBScript: Using For Each...Next Loops for Collections
As you might know, VBScript provides the For...Next
loop that enables you to cycle through a chunk of code a specified
number of times. For example, the following code loops 10 times:
For counter = 1 To 10
Code entered here is repeated 10 times
Next counter
A useful variation on this theme is the For Each...Next
loop, which operates on a collection of objects. You don’t need a loop
counter because VBScript loops through the individual elements in the
collection and performs on each element whatever operations are inside
the loop. Here’s the structure of the basic For Each...Next loop:
For Each element In collection
[statements]
Next
element | A variable used to hold the name of each element in the collection |
collection | The name of the collection |
statements | The statements to execute for each element in the collection |
The following code loops through all the arguments specified on the script’s command line and displays each one:
For Each arg In WScript.Arguments
WScript.Echo arg
Next
JavaScript: Using Enumerators and for Loops for Collections
To iterate through a collection in JavaScript, you must do two things: create a new Enumerator object and use a for loop to cycle through the enumerated collection.
To create a new Enumerator object, use the new keyword to set up an object variable (where collection is the name of the collection you want to work with):
var enum = new Enumerator(collection)
Then set up a special for loop:
for (; !enumerator.atEnd(); enumerator.moveNext())
{
[statements];
}
enumerator | The Enumerator object you created |
statements | The statements to execute for each element in the collection |
The Enumerator object’s moveNext method runs through the elements in the collection, whereas the atEnd
method shuts down the loop after the last item has been processed. The
following code loops through all the arguments specified on the script’s
command line and displays each one:
var args = new Enumerator(WScript.Arguments);
for (; !args.atEnd(); args.moveNext())
{
WScript.Echo(args.item());