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:

Include Statements
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.

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>
Contents of fso.js:
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;
} 
Multiple-Engine Support
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>
Type Library Support
In the following example, MyComponent was developed with Microsoft® Visual Basic® 5.0. MyComponent defines the constant MyError with the following statement.
Public Const MyError = "You are not using MyComponent correctly."
The type library is contained in mycomponent.lib, which you have installed in C:\MyComponent.
<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>
Tools Support
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.
Multiple Jobs in One File
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