PC Magazine

  PC Tech

The Windows Scripting Host

Introduction

Active Scripting

The Windows Scripting Host Object Model

The WshShell and WshNetwork Objects

Putting It Together

Figure 1: Mapping Network Drives

Figure 2: Creating URL Shortcuts



X10.com - The SuperSite for Home Automation!

NextCard Visa- 2.9% intro APR -Apply Now

 
  Categories:
Scripting Languages
Windows 95/NT
Operating Systems
The Windows Scripting Host
Putting It Together

Continued from The WshShell and WshNetwork Objects

To demonstrate how to use the functionality of the Windows Scripting Host, we'll walk through the creation of a script that reads a list of UNC names from a text file and maps a network drive to each name (see Figure 1). UNC stands for Universal Naming Convention, and it is used to identify folders shared on the network. For instance, if your computer is named "MyWin95" and you have shared a folder as "Downloads," the UNC name for that folder would be "\\MyWin95\

Downloads."

This demo uses VBScript, but it could be implemented in any Active Scripting language. After running the script, you should see new drives in Windows Explorer that correspond to the names in your List.txt file.

The first thing we need to do is open the text file that contains the UNC names. VBScript has two built-in objects we'll use to read a file, FileSystemObject and TextStream. The following two lines of code handle the opening of an existing file:

Set fs = WScript.CreateObject ("Scripting.FileSystemObject")

Set listFile = fs.OpenTextFile ("c:\wsh\list.txt")

The first line calls WScript.CreateObject to get a new FileSystemObject. Then the new FileSystemObject's OpenTextFile method is called and returns a TextStream object. This TextStream object will be used for the rest of the script to read text from the file, one line at a time. The next thing we need to do is initialize a WshNetwork, like so:

Set WshNetwork = WScript.CreateObject ("WScript.Network")

With the initial setup complete, it is time to implement the main loop. Even if you've never used VBScript, if you have a general understanding of programming or scripting, you should be able to follow this. The first section of the loop is the Do While:

Do While listFile.AtEndOfStream <>
True

This keeps the loop cycling as long as we haven't reached the end of the file. AtEndOfStream is a property of TextStream that automatically gets set to True when the last line of the file is read. The next line of code actually reads a single line of text from the file and inserts it into a variable:

nameString = listFile.ReadLine

The following For loop ensures that any available drive letter will be used. Notice that we start at position 26 (Z) and move backward to position 5 (E). The expression Chr(driveNum + 64) translates the drive number into the corresponding capital letter. Also, normally, if a user attempts to map a network drive to a letter that already exists, an error message would appear and execution of the script would halt; the On Error Resume Next statement causes this message to be suppressed so that script execution can continue uninterrupted.

For driveNum = 26 to 5
Step -1

driveString = Chr (driveNum + 64) & ":"

On Error Resume Next

WshNetwork.MapNetworkDrive driveString, nameString

If Err.Number = 0 Then Exit For

Next

If the driveNum variable reaches a value of 5, then no available drive letters have been found. This is a bad situation, so displaying a dialog in order to let the user know about it is a good idea.

If driveNum <= 5 Then

WshShell.Popup("No drive letters available.")

End If

At this point, the only things that are left to do are to end the loop with the Do While statement and to close the TextStream that we used to read the UNC names.

Loop

listFile.Close

This sample merely scratches the surface of possibilities. The functionality that the Windows Scripting Host object model exposes, combined with the power of being able to script any application that supports OLE Automation, should give quite a few IS shops a big boost.

For those who do not have access to a LAN, Figure 2 shows another example. This script--which reads URLs from a text file and creates URL shortcuts on the desktop--works much as the UNC sample does, with a few minor differences. The biggest difference is that it reads two lines of text for each shortcut: The first line is the name of the shortcut on the desktop, and the second line is the URL. For example, to create a shortcut to PC Magazine's home page, you'd include the following two lines in Url.txt:

PC Magazine's Home page

http://www.pcmag.com

Both scripts are available for downloading on PC Magazine's Web site.

The Windows Scripting Host is a rather low-profile product, and if you haven't been paying close attention, you may have missed it. The documentation is weak and, in many cases, does not match the actual methods available. All these factors lead me to believe that Microsoft is missing the boat on this one. Why go to the trouble of implementing this technology without really letting people know about it? Only Bill can answer that question.

As a developer, I have found the Windows Scripting Host to be extremely useful. I use it for everything from mucking about with Registry entries to making it easy for my girlfriend to map drives on the LAN in my home. The Windows Scripting Host seems to be a rapidly evolving piece of software, and hopefully future revisions will contain enhanced functionality.

If you want to get a copy of the Windows Scripting Host, you can find it in several places. The latest version of the Internet Client SDK (a.k.a. Platform SDK, ActiveX SDK, and Sweeper) includes it. If you don't want to download over 100MB just to get it, you can also find it at www.microsoft.com/management/WSH.htm. Happy scripting!

Paul Modzelewski is a software engineer at Group Logic. You can reach him at paul@moon.jic.com.

Continues. . .

Published as Operating Systems in the 02/10/98 issue of PC Magazine.


 SPONSORED LINKS
@Backup   Your Solid Online Backup Plan. Download Now.
Services   9c/MINUTE LONG DISTANCE, 5c/MINUTE ON SUNDAYS!
STORAGE   Quantum means non-stop business, 24 hours a day
Software   X10.com -- The SuperSite for Home Automation
Books   Bargain Books up to 90% off at barnesandnoble.com
 ZDNET FEATURED LINKS
Downloads   Check out the best new downloads in Reviewer's Raves
Bargains!   Shop the Basement for best buys on computer products
Free Help   Got computing questions? ZDHelp has all the answers!
 MAGAZINE OFFERS
Free Offer   Get a FREE SUBSCRIPTION to Inter@ctive Week

TOP
Copyright (c) 1998 Ziff-Davis Inc.