|
November 1999 http://msdn.microsoft.com/library/periodic/period99/w9p99b4.htm
Automating Windows tasks using Windows Scripting HostDo you remember DOS batch files? Batch files are routines that automate certain repetitive tasks. Unfortunately, Windows never offered an equivalent feature; that is, until recently. Windows Scripting Host is a new language-independent scripting host for Windows 9x and NT that lets you build batch applications with Visual Basic Scripting Edition and JScript routines.In this article, we'll discuss some benefits of Windows Scripting Host, take a closer look at the host, scripting engine, and object collection, and look at the basics of writing and running scripts. We'll also provide a sample script to give you an idea of what's possible with Windows Scripting Host. Windows Scripting Host's benefitsIn the past, the only scripting language supported by Windows was the DOS operating system command language. This was used to create batch files. However, the DOS language has limited features when compared to the VBScript or JScript languages. While DOS scripts are still supported by Windows Scripting Host, today's ActiveX scripting allows users to write more complex script.Both VBScript and JScript languages allow two-way branching. This means that using a true If/Else statement, a script can make decisions. The script might execute one set of commands if a given condition is true, or another set if the condition is false. In addition, both languages are good at performing math operations, including common trigonometry functions. Another benefit of Windows Scripting Host is that it allows scripts to be executed directly on the Windows desktop, without embedding those scripts in an HTML document. Scripts are run on the desktop by clicking a script file or entering a command in DOS. Windows Scripting Host is a low-memory host that's perfect for Windows task automation. The host and script engineAs good as Windows Scripting Host's scripting languages are, they don't allow their scripts to do very much. They can't affect anything outside their own script. The languages don't allow reading or writing disk files, displaying information on the monitor, updating the Registry, or changing the computer in any way.This is why scripts run under a special program-the host. The host reads the script, interprets the script's language, and calls another program to execute the script's instructions. This program is a language-specific scripting engine. In addition, the host allows scripts to access objects embedded in the host. Familiar to programmers, an object is a collection of subroutines (methods) and variables (properties) that are available to other programs. These methods and variables are usually related. Windows Scripting Host's object collectionWindows Scripting Host's collection contains four objects-Wscript , WshShell , WshNetwork , and FileSystemObject . Each object has a variety of methods and properties.
The
Table A: Important properties and methods for the WScript object
The
Table B: Some WshShell methods and properties
The
Table C: Key properties and methods for the WshNetwork object
The last Windows Scripting Host object is
Table D: Properties and methods for the FileSystemObject perform disk operations
What you need to get startedThe first thing to do is make sure you have Windows Scripting Host. If you're running Windows 98 or Internet Information Server 4.0, you already have it. Windows 95 users can download Windows Scripting Host from the Microsoft Web site at msdn.microsoft.com/scripting/.Microsoft doesn't provide a visual editor for creating Windows Scripting Host scripts. However, Notepad works well for this application. Creating and running scriptsCompared to some programming experiences, the creation of Windows Scripting Host scripts is relatively simple. In Notepad, you write a script using objects like those in Tables A, B, C, and D. Then simply save your file.
Running scripts is also an easy task. There are a couple different ways to do this. First, you could use the command-based version of Windows Scripting Host from the DOS prompt. This version, C:\WINDOWS\CSCRIPT.EXE, gives you control over how a script executes. This is accomplished by adding parameters to the DOS command. Host parameters enable or disable Windows Scripting Host options, and are always preceded by two slashes (//). The script name is always the name for script file, and script parameters are passed to the script. Script parameters are always preceded by one slash (/). Table E lists host parameters supported by CSCRIPT.EXE.
Table E: Host parameters support by CSCRIPT.EXE
If you don't want to specify parameters when you run a script, use the Windows-based version of Windows Scripting Host. There are three ways to run scripts with this version. One way to run scripts with this version is to simply double-click on files and icons in My Computer, Windows Explorer, and Find windows. Another method is to select Run from the Start menu, then type the full name of script you want to run in the Open text box. The final option is to run WSCRIPT.EXE from the Run command, specifying the script name and any optional parameters. Sample scriptLet's look at a sample script written in VBScript. The particular example in Listing A was provided by Microsoft, and can be downloaded from their Web site (along with several others) at msdn.microsoft.com/scripting/.This script creates a shortcut to Notepad on your desktop. It should be noted that the scope of this article won't teach you VB or Java programming. However, this example will give you an idea of what Windows Scripting Host can accomplish with some basic programming knowledge.
Listing A: Shortcut.vbs
ConclusionWindows Scripting Host is a strong successor to the batch files of the past. In addition to being supported by all Win32 programs, a growing number of outside vendors support the product and provide additional scripting engines, hosts, etc. With a little programming skill, you can write your own scripts to automate Windows functions. Even if you don't feel comfortable writing your own scripts, there are plenty available for download from the Web. Trust me, Windows Scripting Host scripts will make your life easier!
Send feedback on this article. Find support options. © 2000 Microsoft Corporation. All rights reserved. Terms of use. |
+ NASA Privacy Statement, Disclaimer, and Accessibility Certification This site last updated November 10, 2008. |