Microsoft® Windows® Script Host Using Windows Script Files |
WSH Tutorial Previous Next |
The Windows script (.wsf) file, a text document containing Extensible Markup Language (XML) code, incorporates several features that offer you increased scripting flexibility. Because Windows script files are not engine-specific, they can contain script from any ActiveX®-compliant scripting engine.With .wsf files, you can take advantage of the following benefits as you create your scripts:
- Support for Include statements - allows you to incorporate previously-written functions from VBScript or JScript files into your Windows Script Host project.
- Support for multiple engines - allows you to use more than one scripting language per file.
- Support for Type libraries - allows you to add constants to your code.
- Support for tools - allows you to edit files with any XML editor.
- Support for multiple jobs in one file - allows you to store all of your code in a single location.
If you have .js and .vbs files from previous Windows Script Host projects, a .wsf file enables you to use them with Windows Script Host. A .wsf file encapsulates a library of functions that can in turn be used by multiple .wsf files.Contents of fso.js:The following example shows the contents of a .wsf file that includes a JScript file (fso.js), plus a VBScript function that calls a function (GetFreeSpace) in the included file. The contents of fso.js are provided also.
<Job id="IncludeExample"> <script language="JScript" src="FSO.JS"/> <script language="VBScript"> ' Get the free space for drive C. s = GetFreeSpace("c:") WScript.Echo s </Script> </Job>
function GetFreeSpace(drvPath) { var fs, d, s; fs = new ActiveXObject("Scripting.FileSystemObject"); d = fs.GetDrive(fs.GetDriveName(drvPath)); s = "Drive " + drvPath + " - " ; s += d.VolumeName; s += " Free Space: " + d.FreeSpace/1024 + " Kbytes"; return s; }
Since one scripting language may not have all the functionality you need, Windows Script Host allows you to combine multiple languages in a single .wsf file. The following example shows the contents of a .wsf file that includes both VBScript® and PerlScript code:
<job id="PERLandVBS"> <script language=PerlScript RUNAT=Server> sub PerlHello { my $str = @_[0]; $WScript->Echo($str); } </script> <script language="VBScript"> WScript.Echo "Hello from VBScript" PerlHello "Hello from PERLScript" </Script> </Job>
In the following example, MyComponent was developed with Microsoft® Visual Basic® 5.0. MyComponent defines the constant MyError with the following statement.The type library is contained in mycomponent.lib, which you have installed in C:\MyComponent.Public Const MyError = "You are not using MyComponent correctly."<Job id="IncludeExample"> <Reference progid="MyComponent.MyClass"> <Script language="VBScript"> Dim MyVar Set MyVar = CreateObject("MyComponent.MyClass") Currentreturn = MyVar.MyMethod If Currentreturn = False then WScript.Echo MyError End If </Script> </Job>
Since the .wsf file is in XML format, you can use any editor that supports XML to edit .wsf files. You can also use any text editor, such as Notepad.
Instead of keeping all your scripts in separate files, you can incorporate them all into one .wsf file and break them into several different jobs. You can then run each job separately using syntax similar to the following example, where MyFirstJob is the name of the job contained in the MyScripts.wsf file:CScript //Job:MyFirstJob MyScripts.wsf