How to create Windows scripts by using Visual Studio 2005 or Visual Studio .NET (827075)
The information in this article applies to:
- Microsoft Visual Studio 2005 Standard Edition
- Microsoft Visual Studio 2005 Professional Edition
- Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2003), Academic Edition
- Microsoft Visual Studio .NET (2002), Professional Edition
- Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2002), Academic Edition
SUMMARYThis 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 topRequirementsThe 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 topCreate a Simple JScript File (JScript1.js)- Start Windows Explorer.
- In a convenient location, create a folder that is named
MyFolder.
- Start Visual
Studio 2005 or Visual Studio .NET.
- On the File menu, point to
New, and then click File. The New
File dialog box appears.
- Under Categories, click
Script.
- Under Templates, click
JScript File.
- Click Open. By default, the JScript1 file
is created.
- In the JScript1 file, locate the following code:
// JScript source code - Paste the following code after the code that you located in
step 8:
// Display a message.
WScript.Echo("Simple JScript File"); - On the File menu, click Save
JScript1 As. The Save File As dialog box appears.
- In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2.
- Click Save. The JScript1.js file is
created in MyFolder.
- On the File menu, click
Close.
back to the topCreate a Simple VBScript File (VBScript1.vbs)- 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.
- Under Categories, click
Script.
- Under Templates, click
VBScript File.
- Click Open. By default, the VBScript1 file
is created.
- In the VBScript1 file, locate the following code:
' VBScript source code - Paste the following code after the code that you located in
step 5:
' Display a message.
WScript.Echo "Simple VBScript File" - On the File menu, click Save
VBScript1 As. The Save File As dialog box appears.
- 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.
- Click Save. The VBScript1.vbs file is
created in MyFolder.
- On the File menu, click
Close.
back to the topCreate a JScript File (JScript2.js) That Contains a Function- 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.
- Under Categories, click
Script.
- Under Templates, click
JScript File.
- Click Open. By default, the JScript2 file
is created.
- In the JScript2 file, locate the following code:
// JScript source code - Paste the following code after the code that you located in
step 5:
// MyFunction is a function that returns "MyFunction".
function MyFunction(){
return "MyFunction";
} - On the File menu, click Save
JScript2 As. The Save File As dialog box appears.
- 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.
- Click Save. The JScript2.js file is
created in MyFolder.
- On the File menu, click
Close.
back to the topCreate a Windows Script Host File (WindowsScript1.wsf)- 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.
- Under Categories, click
Script.
- Under Templates, click
Windows Script Host.
- Click Open. By default, the WindowsScript1
file is created.
back to the topInclude the JScript2.js File in Your Windows Script Host File- 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"> - Replace the code that you located in step 1 with the
following code:
<script language="JScript" src="JScript2.js"> - In the WindowsScript1 file, locate the following code:
WScript.Echo "VBScript" - 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 - On the File menu, click Save
WindowsScript1 As. The Save File As dialog box appears.
- 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.
- Click Save. The WindowsScript1.wsf file is
created in MyFolder.
- On the File menu, click
Close.
back to the topRun Your Scripts- Switch to Windows Explorer.
- 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.
- Double-click the JScript1.js file. A
Windows Script Host message box appears that contains the following
text:Simple JScript File
- Click OK to close this
message box.
- Double-click the VBScript1.vbs file. A
Windows Script Host message box appears that contains the following
text:Simple VBScript File
- Click OK to close this
message box.
- Double-click the WindowsScript1.wsf file. A
Windows Script Host message box appears that contains the following
text:JScript
- Click OK to close this
message box. A
Windows Script Host message box appears that contains the
following text:VBScript
- Click OK to close this
message box. A
Windows Script Host message box appears that contains the
following text:MyFunction
- Click OK to close this
message box.
back to the
topREFERENCESFor more information, visit the following Microsoft Developer Network (MSDN) Web
sites: back to the
top
Modification Type: | Major | Last Reviewed: | 3/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbDebug kbSample kbcode kbScript kbProgramming kbHOWTOmaster KB827075 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|