Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Scripting
Windows Script Host
 Using Windows Script Files (.wsf)
This page is specific to
.NET Framework 3.0

Other versions are also available for the following:
Windows Script Host
Using Windows Script Files (.wsf)

Updated: November 2007

A Windows script (*.wsf) file is a text document containing Extensible Markup Language (XML) code. It incorporates several features that offer you increased scripting flexibility. Because Windows script files are not engine-specific, they can contain script from any Windows Script compatible scripting engine. They act as a container.

.wsf files support

You can

Include statements

Incorporate functions from VBScript or JScript files into your Windows Script Host project.

Multiple engines

Use more than one scripting language per file.

Type libraries

Add constants to your code.

Tools

Edit files with any XML editor.

Multiple jobs in one file

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.

The following example shows 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 also shown.

<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>

The fso.js file contains the following:

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 a .wsf file that includes both VBScript and PerlScript code:

<job id="PERLandVBS">
   <script language="PerlScript">
      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.

Public Const MyError = "You are not using MyComponent correctly"

The type library is contained in mycomponent.lib, which is 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>

Since the .wsf file is in XML format, you can use any editor that supports XML to edit .wsf files. This includes text editors, 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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
&lt;script&gt;      FredMayer ... Timothy Tannyer   |   Edit   |  
Finish the block with </script>, not <script>

The first script block is incorrect and should be updated
Clarification      Timothy Tannyer   |   Edit   |  
This article states that: "a Windows script (*.wsf) file is a text document containing Extensible Markup Language (XML) code." While true at one level, the .wsf file contains actual scripts which are encapsulated in XML.
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker