WSH in Other Languages - Jscript
(Page 3 of 5 )
Jscript was also released in 1996 by Microsoft to meet the ECMAScript scripting programming language specifications. It most closely resembles Netscape’s own ECMAScript solution known as JavaScript. While the terms Jscript and JavaScript are often erroneously used interchangeably, it is important to remember that the two represent completely different and independent entities. Despite their close resemblance, Microsoft retained enough differences to keep the two languages incompatible.
Because of Jscript’s wide use as a web development language, its support was also included in WSH natively. This makes it the perfect alternative to VBScript for many administrators.
var fso = new ActiveXObject("Scripting.FileSystemObject");
An object can be accessed by creating a new instance of the object with the ActiveXObject method. Just as in VBScript, an object is typically identified by its ProgID.
var folder = fso.GetFolder("C: emp");
Like VBScript, Jscript uses standard object dot syntax to access an object’s properties and methods.
Working with Collections
Unlike VBScript, Jscript cannot access collection objects directly. You must first create an Enumerator object.
var e = new Enumerator(folder.files);
An enumerator object provides a mechanism for accessing and manipulating the objects found within a collection.
for (e.moveFirst(); ! e.atEnd(); e.moveNext()) {
var file = e.item();
WScript.Echo(file.name);
}
Iterating through the enumerator’s items is then done using a For structure.
Additional Notes About Jscript
All statements in Jscript require a semi-colon line ending and parenthesis should be used to denote all methods even when you are not supplying parameters.
Unlike VBScript, Jscript does maintain some case sensitivity. Variables and object names are case sensitive, while an object’s property and method names are not.
It is also important to remember that the forward slash is a reserved character and must be escaped whenever it appears in a string.
Next: Python >>
More Windows Scripting Articles
More By Nilpo