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.
Filename extension | .wsf |
---|---|
Internet media type | text/xml |
Developed by | Microsoft |
Type of format | Scripting |
Container for | Scripts |
Error isolation
editA 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" ?>
<job id="Partially works">
<!-- This will not work -->
<script language="VBScript">
' <![CDATA[
WScript.echo 4/0 ' Oh, boy! You cannot divide by zero...
' ]]>
</script>
<!-- This will work... definitely... -->
<script language="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
editA 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 -->
<job id="SORT-VBScriptWithJScript">
<script language="JScript">
function SortVBArray(arrVBArray) {return arrVBArray.toArray().sort();}
</script>
<script language="VBScript">
' <![CDATA[
'** Fastest sort: call the Jscript sort from VBScript
myData = "a,b,c,1,2,3,X,Y,Z,p,d,q"
wscript.echo "Original List of values: " & vbTab & myData
starttime = 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,q
JScript sorted in 0 seconds: 1,2,3,X,Y,Z,a,b,c,d,p,q
Exposing constants
editAnother 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:
const adLockBatchOptimistic = 4
MsgBox "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 Reference
Notes 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>
<job id="EnumerateConstantsADO">
<reference object="ADODB.Recordset" />
<script language="VBScript">
' <![CDATA[
dim title, str, i
ctecArray = Array("adOpenUnspecified","adOpenForwardOnly", _
"adOpenKeyset","adOpenDynamic","adOpenStatic")
title = "ADO Recordset Values for Constants"
str = title & vbNewLine & vbNewLine
str = str & "*CursorTypeEnum Constants*" & vbNewLine
For i = 0 to ubound(ctecArray)
str = str & Eval(ctecArray(i)) & vbTab & ctecArray(i) & vbNewLine
Next
str = str & vbNewLine
str = str & "*LockTypeEnum Constants*" & vbNewLine
ltecArray = Array("adLockUnspecified","adLockReadOnly", _
"adLockPessimistic","adLockOptimistic", _
"adLockBatchOptimistic")
For i = 0 to ubound(ltecArray)
str = str & Eval(ltecArray(i)) & vbTab & ltecArray(i) & vbNewLine
Next
MsgBox str, 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 adOpenUnspecified
0 adOpenForwardOnly
1 adOpenKeyset
2 adOpenDynamic
3 adOpenStatic
*LockTypeEnum Constants*
-1 adLockUnspecified
1 adLockReadOnly
2 adLockPessimistic
3 adLockOptimistic
4 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
editExternal links
edit- Using Windows Script Files - From The WayBack Machine's archive of a page from Microsoft's website
- Scripting Languages Available in the Script Center - From The WayBack Machine's archive of a page from Microsoft's website