How to create Windows scripts by using Visual Studio 2005 or Visual Studio .NET

Summary

This step-by-step article describes how to create Microsoft Windows scripts by using Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. You can run a Windows script by double-clicking the script file in Windows Explorer.

Microsoft Windows Script Host (WSH) is a language-independent scripting host for script engines that are compatible with Windows scripts. WSH creates an environment for hosting scripts. It runs text files that are stand-alone scripts. When you run a script, WSH provides objects and services for the script. You can use WSH to run scripts both on the Windows desktop and at a command prompt. WSH is built into Microsoft Windows 98 or later.

Typically, Windows scripts are written in either Microsoft JScript or Microsoft Visual Basic Scripting Edition (VBScript). The script engines for these programming languages are included with Windows 98 or later. A stand-alone script that is written in JScript has the .js file name extension. A stand-alone script that is written in VBScript has the .vbs file name extension. These file name extensions are registered with Windows.

You may also write Windows scripts in other languages, such as Perl, REXX, and Python. However, you must register the script engines for these programming languages with Windows. When you run a registered Windows script, Windows starts WSH. WSH activates the associated script engine to interpret and to run the script.

You can use Visual Studio 2005 or Visual Studio .NET to develop scripts in JScript and VBScript. You can also use Visual Studio 2005 or Visual Studio .NET to develop WSH files. A WSH file has the .wsf file name extension. WSH files may include other script files as parts of the script. Therefore, multiple WSH files may reference libraries of useful functions that are created and stored in one location. Additionally, you can combine JScript and VBScript in a single WSH file.

You can also use Visual Studio 2005 or Visual Studio .NET to debug scripts. However, when you install Visual Studio 2005 or Visual Studio .NET, you automatically install the Visual Studio 2005 or Visual Studio .NET debugger as the default debugger for scripts. Therefore, the Visual Studio 2005 or Visual Studio .NET debugger replaces the Windows Script Debugger in applications such as Internet Explorer.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
  • Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topic:
  • Microsoft Windows Script Technologies
back to the top

Create a Simple JScript File (JScript1.js)

  1. Start Windows Explorer.
  2. In a convenient location, create a folder that is named MyFolder.
  3. Start Visual Studio 2005 or Visual Studio .NET.
  4. On the File menu, point to
    New, and then click File. The New File dialog box appears.
  5. Under Categories, click
    Script.
  6. Under Templates, click JScript File.
  7. Click Open. By default, the JScript1 file is created.
  8. In the JScript1 file, locate the following code:
    // JScript source code
  9. Paste the following code after the code that you located in step 8:
    // Display a message.
    WScript.Echo("Simple JScript File");
  10. On the File menu, click Save JScript1 As. The Save File As dialog box appears.
  11. In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2.
  12. Click Save. The JScript1.js file is created in MyFolder.
  13. On the File menu, click
    Close.
back to the top

Create a Simple VBScript File (VBScript1.vbs)

  1. In Visual Studio 2005 or in Visual Studio .NET, point to
    New on the File menu, and then click
    File. The New File dialog box appears.
  2. Under Categories, click
    Script.
  3. Under Templates, click VBScript File.
  4. Click Open. By default, the VBScript1 file is created.
  5. In the VBScript1 file, locate the following code:
    ' VBScript source code
  6. Paste the following code after the code that you located in step 5:
    ' Display a message.
    WScript.Echo "Simple VBScript File"
  7. On the File menu, click Save VBScript1 As. The Save File As dialog box appears.
  8. In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2 of the "Create a Simple JScript File (JScript1.js)" section of this article.
  9. Click Save. The VBScript1.vbs file is created in MyFolder.
  10. On the File menu, click
    Close.
back to the top

Create a JScript File (JScript2.js) That Contains a Function

  1. In Visual Studio 2005 or in Visual Studio .NET, point to
    New on the File menu, and then click
    File. The New File dialog box appears.
  2. Under Categories, click
    Script.
  3. Under Templates, click JScript File.
  4. Click Open. By default, the JScript2 file is created.
  5. In the JScript2 file, locate the following code:
    // JScript source code
  6. Paste the following code after the code that you located in step 5:
    // MyFunction is a function that returns "MyFunction".
    function MyFunction(){
    return "MyFunction";
    }
  7. On the File menu, click Save JScript2 As. The Save File As dialog box appears.
  8. In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2 of the "Create a Simple JScript File (JScript1.js)" section of this article.
  9. Click Save. The JScript2.js file is created in MyFolder.
  10. On the File menu, click
    Close.
back to the top

Create a Windows Script Host File (WindowsScript1.wsf)

  1. In Visual Studio 2005 or in Visual Studio .NET, point to
    New on the File menu, and then click
    File. The New File dialog box appears.
  2. Under Categories, click
    Script.
  3. Under Templates, click Windows Script Host.
  4. Click Open. By default, the WindowsScript1 file is created.
back to the top

Include the JScript2.js File in Your Windows Script Host File

  1. On the View menu in Visual Studio 2005 or in Visual Studio .NET, click HTML Source, and then locate the following code in the WindowsScript1 file:
    <script language="JScript">
    In Visual Studio .NET 2002, locate the following code in the WindowsScript1 file:
    <script language="JScript">
  2. Replace the code that you located in step 1 with the following code:
    <script language="JScript" src="JScript2.js">
  3. In the WindowsScript1 file, locate the following code:
    WScript.Echo "VBScript"
  4. Paste the following code after the code that you located in step 3:
    ' Retrieve the return value of the MyFunction function _
    ' into the retVal variable.
    retVal = MyFunction()
    ' Display the retrieved return value.
    WScript.Echo retVal
  5. On the File menu, click Save WindowsScript1 As. The Save File As dialog box appears.
  6. In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2 of the "Create a Simple JScript File (JScript1.js)" section of this article.
  7. Click Save. The WindowsScript1.wsf file is created in MyFolder.
  8. On the File menu, click
    Close.
back to the top

Run Your Scripts

  1. Switch to Windows Explorer.
  2. Locate and then click the MyFolder folder that you created in step 2 of the "Create a Simple JScript File (JScript1.js)" section of this article.
  3. Double-click the JScript1.js file. A
    Windows Script Host message box appears that contains the following text:
    Simple JScript File
  4. Click OK to close this message box.
  5. Double-click the VBScript1.vbs file. A
    Windows Script Host message box appears that contains the following text:
    Simple VBScript File
  6. Click OK to close this message box.
  7. Double-click the WindowsScript1.wsf file. A Windows Script Host message box appears that contains the following text:
    JScript
  8. Click OK to close this message box. A
    Windows Script Host message box appears that contains the following text:
    VBScript
  9. Click OK to close this message box. A
    Windows Script Host message box appears that contains the following text:
    MyFunction
  10. Click OK to close this message box.
back to the top
Propriedades

ID do Artigo: 827075 - Última Revisão: 13 de mai de 2008 - Revisão: 1

Comentários