Windows Script File

Last updated
Windows Script File
Filename extension
.wsf
Internet media type text/xml
Developed by Microsoft
Type of formatScripting
Container for Scripts

A Windows Script File (WSF) is a file type used by the Microsoft Windows Script Host. It allows mixing the scripting languages JScript and VBScript within a single file, or other scripting languages such as Perl, Object REXX, Python, or Kixtart if installed by the user. These types of scripts may also be used to link many other external scripts together using a src parameter on the <script> tag in a manner similar to HTML. Windows Script Files have the extension ".WSF". A WSF makes reference to each script module in a very basic XML hierarchy as shown below, adhering to those standards outside the <script> tags. Literal use of "</script>" or "<script>" inside your <script> tags and similar challenges can be handled by the use of CDATA, as shown within the examples.

Contents

Error isolation

A WSF may be useful for isolating errors. Its modular nature prevents one script reference from interfering with another. Here is a WSF example with one module that produces an error and one that does not:

<?xml version="1.0" ?><jobid="Partially works"><!-- This will not work --><scriptlanguage="VBScript">'    <![CDATA[WScript.echo4/0' Oh, boy! You cannot divide by zero...'    ]]></script><!-- This will work... definitely... --><scriptlanguage="VBScript">'    <![CDATA[WScript.echo"Hello, Scripters!"&vbNewline&_ "Fantastic! It worked!"'    ]]></script></job>

The first script module will produce a "divide by zero" error. Typically this would cause the script to end in the Windows Script Host but this modular method allows the script to continue and execute the second script module.

Mixed language support

A Windows Script File supports multiple languages, as described on the Windows Script Host reference. One of the features of this file format is that you may use more than one at once. This means you can have one scripting language use code from another scripting language. The most memorable example for long-time VBScript users is the use of Microsoft JScript to service a sort request for VBScript since it does not have a built-in sort function for an array of values. VBScript users may write their own sort method or borrow one from an existing object like an ADO (ActiveX Data Objects) Recordset or a .NET (.NET Framework) ArrayList, but the fastest way to sort an array is to use the method built into JScript. Here is a basic example of how that works:

<?xml version="1.0" ?><!-- Mixing JScript and VBScript --><jobid="SORT-VBScriptWithJScript"><scriptlanguage="JScript">functionSortVBArray(arrVBArray){returnarrVBArray.toArray().sort();}</script><scriptlanguage="VBScript">'    <![CDATA['** Fastest sort: call the Jscript sort from VBScriptmyData="a,b,c,1,2,3,X,Y,Z,p,d,q"wscript.echo"Original List of values: "&vbTab&myDatastarttime=timer()sortedArray=SortVBArray(split(myData,","))endtime=timer()jscriptTime=round(endtime-starttime,2)wscript.echo"JScript sorted in "&jscriptTime&" seconds: "&vbTab&sortedArray'    ]]></script></job>

The output looks like this, sorted by ASCII code sequence:

Original List of values:        a,b,c,1,2,3,X,Y,Z,p,d,qJScript sorted in 0 seconds:    1,2,3,X,Y,Z,a,b,c,d,p,q

Exposing constants

Another very useful feature of a WSF is that the XML wrapper can be bound to an object reference or control so you can use that object's constants instead of having to declare them. In regular VBScript and JScript files, you would be forced to declare a constant's value (outside those that are internal to the Windows Script Host) in order to use the constant. An example of this is shown below:

constadLockBatchOptimistic=4MsgBox"The value of ""adLockBatchOptimistic"" is "&_adLockBatchOptimistic&".",vbInformation,"adLockBatchOptimistic"

If your object documentation only refers to the constant's name and not the constant's value, you would have no way of knowing the value without the help of an Integrated development environment to tell you what they equate to. By using the WSF reference declaration, you can use the constants without declaring their values. The example below enumerates the values of several common constants in the ADO (ActiveX Data Objects) Recordset.

<?xml version="1.0" ?><!-- WSF Example with Object ReferenceNotes for this very formal example: CDATA is used to help the XML parser ignore  special characters in the content of the script.   The CDATA open and close must be masked  from VBScript by making them comments.--><package><jobid="EnumerateConstantsADO"><referenceobject="ADODB.Recordset"/><scriptlanguage="VBScript">'  <![CDATA[dimtitle,str,ictecArray=Array("adOpenUnspecified","adOpenForwardOnly",_ "adOpenKeyset","adOpenDynamic","adOpenStatic")title="ADO Recordset Values for Constants"str=title&vbNewLine&vbNewLinestr=str&"*CursorTypeEnum Constants*"&vbNewLineFori=0toubound(ctecArray)str=str&Eval(ctecArray(i))&vbTab&ctecArray(i)&vbNewLineNextstr=str&vbNewLinestr=str&"*LockTypeEnum Constants*"&vbNewLineltecArray=Array("adLockUnspecified","adLockReadOnly",_ "adLockPessimistic","adLockOptimistic",_ "adLockBatchOptimistic")Fori=0toubound(ltecArray)str=str&Eval(ltecArray(i))&vbTab&ltecArray(i)&vbNewLineNextMsgBoxstr,vbInformation,Title'  ]]></script></job></package>

Running the above script from a file with a ".WSF" extension, such as one named "EnumerateConstantsADO.wsf", will produce the result shown below:

ADO Recordset Values for Constants*CursorTypeEnum Constants*-1      adOpenUnspecified0       adOpenForwardOnly1       adOpenKeyset2       adOpenDynamic3       adOpenStatic*LockTypeEnum Constants*-1      adLockUnspecified1       adLockReadOnly2       adLockPessimistic3       adLockOptimistic4       adLockBatchOptimistic

In addition, using the object reference to expose the constants makes writing the script more like writing in a standard programming language. In fact, the contents of the sample script, written in VBScript, will actually compile into a Visual Basic program and run the same way as long as that program uses the same reference to ADODB.

See also

Related Research Articles

Active Server Pages (ASP) is Microsoft's first server-side scripting language and engine for dynamic web pages.

VBScript is a deprecated Active Scripting language developed by Microsoft that is modeled on Visual Basic. It allows Microsoft Windows system administrators to generate powerful tools for managing computers without error handling and with subroutines and other advanced programming constructs. It can give the user complete control over many aspects of their computing environment.

In computing, Microsoft's ActiveX Data Objects (ADO) comprises a set of Component Object Model (COM) objects for accessing data sources. A part of MDAC, it provides a middleware layer between programming languages and OLE DB. ADO allows a developer to write programs that access data without knowing how the database is implemented; developers must be aware of the database for connection only. No knowledge of SQL is required to access a database when using ADO, although one can use ADO to execute SQL commands directly.

JScript is Microsoft's legacy dialect of the ECMAScript standard that is used in Microsoft's Internet Explorer 11 and older.

<span class="mw-page-title-main">Windows Script Host</span> Automation Technology for Windows

The Microsoft Windows Script Host (WSH) is an automation technology for Microsoft Windows operating systems that provides scripting abilities comparable to batch files, but with a wider range of supported features. This tool was first provided on Windows 95 after Build 950a on the installation discs as an optional installation configurable and installable by means of the Control Panel, and then a standard component of Windows 98 and subsequent and Windows NT 4.0 Build 1381 and by means of Service Pack 4. The WSH is also a means of automation for Internet Explorer via the installed WSH engines from IE Version 3.0 onwards; at this time VBScript became means of automation for Microsoft Outlook 97. The WSH is also an optional install provided with a VBScript and JScript engine for Windows CE 3.0 and following and some third-party engines including Rexx and other forms of Basic are also available.

<span class="mw-page-title-main">ActionScript</span> Object-oriented programming language created for the Flash multimedia platform

ActionScript is an object-oriented programming language originally developed by Macromedia Inc.. It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript, though it originally arose as a sibling, both being influenced by HyperTalk. ActionScript code is usually converted to byte-code format by a compiler.

<span class="mw-page-title-main">Microsoft Data Access Components</span> Framework

Microsoft Data Access Components is a framework of interrelated Microsoft technologies that allows programmers a uniform and comprehensive way of developing applications that can access almost any data store. Its components include: ActiveX Data Objects (ADO), OLE DB, and Open Database Connectivity (ODBC). There have been several deprecated components as well, such as the Jet Database Engine, MSDASQL, and Remote Data Services (RDS). Some components have also become obsolete, such as the former Data Access Objects API and Remote Data Objects.

<span class="mw-page-title-main">AutoIt</span>

AutoIt is a freeware programming language for Microsoft Windows. In its earliest release, it was primarily intended to create automation scripts for Microsoft Windows programs but has since grown to include enhancements in both programming language design and overall functionality.

Active Scripting is the technology used in Windows to implement component-based scripting support. It is based on OLE Automation and allows installation of additional scripting engines in the form of COM modules.

Take Command Console (TCC), formerly known as 4DOS for Windows NT (4NT), is a command-line interpreter by JP Software, designed as a substitute for the default command interpreter in Microsoft Windows, CMD.EXE.

In Microsoft Windows applications programming, OLE Automation is an inter-process communication mechanism created by Microsoft. It is based on a subset of Component Object Model (COM) that was intended for use by scripting languages – originally Visual Basic – but now is used by several languages on Windows. All automation objects are required to implement the IDispatch interface. It provides an infrastructure whereby applications called automation controllers can access and manipulate shared automation objects that are exported by other applications. It supersedes Dynamic Data Exchange (DDE), an older mechanism for applications to control one another. As with DDE, in OLE Automation the automation controller is the "client" and the application exporting the automation objects is the "server".

In computer programming, an enumerated type is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. An enumerated type can be seen as a degenerate tagged union of unit type. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.

Microsoft XML Core Services (MSXML) are set of services that allow applications written in JScript, VBScript, and Microsoft development tools to build Windows-native XML-based applications. It supports XML 1.0, DOM, SAX, an XSLT 1.0 processor, XML schema support including XSD and XDR, as well as other XML-related technologies.

<span class="mw-page-title-main">Visual Basic (classic)</span> Event-driven programming language

The original Visual Basic is a third-generation event-driven programming language from Microsoft known for its Component Object Model (COM) programming model first released in 1991 and declared legacy during 2008. Microsoft intended Visual Basic to be relatively easy to learn and use. Visual Basic was derived from BASIC and enables the rapid application development (RAD) of graphical user interface (GUI) applications, access to databases using Data Access Objects, Remote Data Objects, or ActiveX Data Objects, and creation of ActiveX controls and objects.

JScript.Encode is a method created by Microsoft used to encode both server and Client-side JavaScript or VB Script source code in order to protect the source code from copying. JavaScript code is used for creating dynamic web content on many websites, with the source code easily viewable, so this was meant to protect the code.

ActivePerl is a distribution of Perl from ActiveState for Windows, macOS, Linux, Solaris, AIX and HP-UX.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

3DMLW is a discontinued open-source project, and a XML-based Markup Language for representing interactive 3D and 2D content on the World Wide Web.

<span class="mw-page-title-main">Scripting language</span> Programming language for run-time events

A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.