.wsf</code>"},"owner":{"wt":"[[Microsoft]]"},"genre":{"wt":"Scripting"},"container for":{"wt":"Scripts"}},"i":0}}]}'>
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> or
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:
\n <job id=\"Partially works\">\n <!-- This will not work -->\n <script language=\"VBScript\">\n' <![CDATA[\n WScript.echo 4/0 ' Oh, boy! You cannot divide by zero...\n' ]]>\n </script>\n <!-- This will work... definitely... -->\n <script language=\"VBScript\">\n' <![CDATA[\n WScript.echo \"Hello, Scripters!\" & vbNewline & _\n \"Fantastic! It worked!\"\n' ]]>\n </script>\n </job>\n"}}'><?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
edit
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:
\n<!-- Mixing JScript and VBScript -->\n <job id=\"SORT-VBScriptWithJScript\">\n <script language=\"JScript\">\n function SortVBSArray(arr) {return arr.toArray().sort();}\n </script>\n <script language=\"VBScript\">\n' <![CDATA[\n '** Fastest sort: call the Jscript sort from VBScript\n myData = \"a,b,c,1,2,3,X,Y,Z,p,d,q\"\n wscript.echo \"Original List of values: \" & vbTab & myData\n starttime = timer()\n sortedArray = SortVBSArray(split(myData,\",\"))\n endtime=timer()\n jscriptTime = round(endtime-starttime,2)\n wscript.echo \"JScript sorted in \" & jscriptTime & \" seconds: \" & vbTab & sortedArray\n' ]]>\n </script>\n </job>\n"}}'><?xml version="1.0" ?>
<!-- Mixing JScript and VBScript -->
<jobid="SORT-VBScriptWithJScript">
<scriptlanguage="JScript">
functionSortVBSArray(arr){returnarr.toArray().sort();}
</script>
<scriptlanguage="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=SortVBSArray(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:
<?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>
<jobid="EnumerateConstantsADO">
<referenceobject="ADODB.Recordset"/>
<scriptlanguage="VBScript">
' <![CDATA[
dimtitle,str,i
ctecArray=Array("adOpenUnspecified","adOpenForwardOnly",_
"adOpenKeyset","adOpenDynamic","adOpenStatic")
title="ADO Recordset Values for Constants"
str=title&vbNewLine&vbNewLine
str=str&"*CursorTypeEnum Constants*"&vbNewLine
Fori=0toubound(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")
Fori=0toubound(ltecArray)
str=str&Eval(ltecArray(i))&vbTab<ecArray(i)&vbNewLine
Next
MsgBoxstr,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: