Downloads
7824.zip

Creating and updating shortcuts to applications and Internet resources on users' workstations can be time-consuming, especially if you have many users. To automate this time-consuming task, I used Windows Scripting Host (WSH) and VBScript to write the script SWDist.vbs. This script not only saves time but also ensures that all the shortcuts are consistent, accurate, and up-to-date.

SWDist.vbs automatically creates and updates shortcuts on workstations running Windows NT Workstation 4.0. To update a shortcut, you need to first delete the old shortcut, then create a new one. If you try to update a shortcut by simply creating a shortcut with the same name as the existing one, you open, not overwrite, that shortcut.

SWDist.vbs uses an .ini file to obtain all the information it needs to create and delete shortcuts. An .ini file lets you use the same script for different types of shortcuts. SWDist.vbs opens the .ini file in read-only mode, reads a line from the file, and determines which task to complete. SWDist.vbs creates all the shortcuts in subfolders under \all users\start menu\programs in the NT directory. However, you can easily change this location by changing that path in the script.

For tracking, SWDist.vbs records in a log file who is running the script, when that person began running it, and the shortcuts that the script creates and deletes. If you want the script to write to the NT event log, you can install the add-on file stmadmin.dll. (You can download stmadmin.dll from http://cwashington.netreach.net.) However, you must install this file on each workstation and use regsvr32.exe to register the file. You also might need to add code to the script that tests the OS to make sure the script is running on an NT workstation. You can run stmadmin.dll only on an NT workstation because Windows 9x doesn't have event logs.

You can download SWDist.vbs from the Code Library on the Win32 Scripting Journal Web site at http://www.winntmag.com/newsletter/scripting. However, before you use the script, you need to know how the .ini file and SWDist.vbs work.

The .ini File
The .ini file contains all the information that SWDist.vbs needs to create and delete shortcuts. An .ini file can include five types of lines: comment, folder, application shortcut, Internet shortcut, and delete shortcut.

Comment lines start with a single quote. These lines provide instructions or notes. When SWDist.vbs executes, it skips all comment and blank lines in the .ini file.

Folder lines contain the name of the folder that the shortcuts reside in. The folder line's syntax is

\[folder name\]

where folder name is the name of the shortcuts folder. Shortcuts folders are under the \all users\start menu\programs folder in the NT directory.

The application, Internet, and delete shortcut lines instruct SWDist.vbs to create an application shortcut, create an Internet shortcut, and delete an existing shortcut, respectively. Commas separate each element in the shortcut lines.

The application shortcut line's syntax is

app, shortcut name.lnk, program,
  work directory, Windows style,
  icon file, icon number

where

  • app is the keyword that tells SWDist.vbs to create an application shortcut.
  • shortcut name.lnk is the name of the shortcut plus the extension .lnk. Although Windows Explorer doesn't display the .lnk extension, every application shortcut uses this extension.
  • program is the absolute path to the application. (An absolute path is a fully qualified path containing drive and directory information before the filename.) You can use either Uniform Naming Convention (UNC) or drive letter paths.
  • work directory is the absolute path to the working directory. You can use either UNC or drive letter paths.
  • Windows style is the value that specifies how the Window will open. You can choose from three values: 1 (activates a window; if a user minimizes or maximizes the window, the system restores it to its original size and position), 3 (activates and maximizes the window), and 7 (minimizes the window and activates the next top-level window). For application shortcuts, this value is always 1. (For more information about Windows style, see the WSH reference at http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/doc/wsprowindowstyle.htm.)
  • icon file is the absolute path to the file containing the icon for the shortcut. You can use either UNC or drive letter paths.
  • icon number is the number of the icon inside the icon file. The first icon in this file is 0, the second icon is 1, and so on.


  • The Internet shortcut line's syntax is
internet, shortcut name.url, URL
  address
where

  • internet is the keyword that tells SWDist.vbs to create an Internet shortcut.
  • shortcut name.url is the name of the shortcut plus the extension .url. Although Windows Explorer doesn't display the .url extension, every Internet shortcut uses this extension.
  • URL address is the address of the resource that you want the shortcut to point to. This address is typically an http:\\ Internet address.
  • Because Internet shortcuts use the default icon for the workstation's default Web browser, you don't need to specify any icon information.

    The delete shortcut line's syntax is

    del, shortcut name

    where

    • del is the keyword that tells SWDist.vbs to verify the existence of a shortcut and delete that shortcut if it exists.
    • shortcut name is the name of the shortcut.

    Figure 1, page 5, contains an example of a completed .ini file.

    SWDist.vbs
    SWDist.vbs begins like most other scripts. After SWDist.vbs includes the Option Explicit and On Error Resume Next error-handling statements, it declares the constants and declares and initializes the script's variables.

    Next, the script includes code to obtain the .ini file and begin the log. Specifically, this code checks for a command-line parameter that contains the .ini file's path and name. If that parameter is present, the code looks for the .ini file that the parameter specifies. If either the parameter or the file is missing, the code notifies the user with an error message and the script exits.

    If the specified .ini file exists, the code calls the OpenLog subroutine. This subroutine sets up the logging system. OpenLog uses the Windows directory environment variable EnvWinDir to check for the SWDist_Events folder in the Windows directory. If this folder is missing, OpenLog creates the folder. To ensure that the log file doesn't get too large, OpenLog checks the size of the log file. If the file is larger than 128KB, OpenLog deletes the file and recreates it. If the file is 128KB or smaller, OpenLog opens and appends events to the file. Whether the file is new or appended, OpenLog starts the log with the date and time, the NT user ID of the person running SWDist.vbs, and the .ini filename.

    With the preparatory work finished, SWDist.vbs uses COM objects from the Microsoft Scripting Run-Time Library's FileSystemObject (FSO) object model to open and read the .ini file. (For information about the FSO object model, see Dino Esposito, "Understanding VBScript: Manipulating Files with FileSystemObject," page 8.) As the SWDist.vbs excerpt in Listing 1 shows, a Do...Loop statement reads in a line from the .ini file, trims the leading and trailing spaces from that line, and passes that line to the CreateShortcut subroutine. This loop continues until the end of the .ini file.

    The CreateShortcut subroutine performs most of the work. As Listing 2 shows, this subroutine uses two nested Select Case statements to test each line in the .ini file and perform the appropriate task. CreateShortcut uses the first Select Case statement to determine the course of action, depending on whether an .ini line is a comment line, blank line, folder name line, or shortcut line. If the first character in the .ini line is a single quote (i.e., a comment line) or empty (a blank line), the subroutine skips that line. If the first character is an opening square bracket (i.e., a folder name line), the subroutine removes the brackets from the folder name and passes it to the strFolderName variable.

    All other lines go through the Case Else clause. As callout A in Listing 2 shows, CreateShortcut uses the Split command (with the comma as the separator) to convert the .ini line into the g_arrShortCutParms array. The number of values in the array depends on the number of elements in the .ini line. For example, an .ini line with the syntax

    app, shortcut name.lnk, program,
      work directory, Windows style,
      icon file, icon number

    has seven values. The first array value g_arrShortCutParms(0) is app, the second array value g_arrShortCutParms(1) is shortcut name.lnk, the third array value g_arrShortCutParms(2) is program, and so on. (If you're unfamiliar with how VBScript arrays work, see Dino Esposito, "Understanding VBScript: Arrays," July 1999.)

    Next, CreateShortcut uses another Select Case statement to determine whether g_arrShortCutParms(0) is app, internet, or del. If the first array value is app, CreateShortcut calls the CreateLink subroutine to create an application shortcut. If the first array value is internet, CreateShortcut calls the CreateURLLink subroutine to create an Internet shortcut. If the first array value is del, CreateShortcut checks to see whether the shortcut exists. If it does, CreateShortcut calls the DeleteLink subroutine to delete that shortcut.

    Before CreateLink, CreateURLLink, and DeleteLink can create or delete a shortcut, CreateShortcut must provide them with the information they need to perform their tasks. CreateShortcut provides CreateLink with five parameters: the new shortcut's absolute path, the application's absolute path, the working directory's absolute path, the Windows style, and the icon's filename and number. CreateShortcut provides the shortcut path parameter by appending the value in strAllUserProgram (i.e., the path to the \all users\start menu\programs\ folder), the value in strFolderName (i.e., the folder name), and the g_arrShortCutParms(1) value (i.e., shortcut name.lnk). CreateShortcut uses the g_arrShortCutParms(2) value for the application path parameter, the g_arrShortCutParms(3) value for the working directory path parameter, and the g_arrShortCutParms(4) value for the Windows-style parameter. CreateShortcut provides the icon filename and number parameter by appending the g_arrShortCutParms(5) value (i.e., the icon file), a comma, and the g_arrShortCutParms(6) value (i.e., the icon number). To remove the extra spaces from all the g_arrShortCutParms values, CreateShortcut uses VBScript's Trim function.

    CreateShortcut provides CreateURLLink with two parameters: the new shortcut's absolute path and the URL path to the resource. CreateShortcut provides the shortcut path parameter by appending the value in strAllUserProgram, the value in strFolderName, and the g_arrShortCutParms(1) value (i.e., shortcut name.url). CreateShortcut uses the g_arrShortCutParms(2) value for the URL path parameter.

    CreateShortcut provides DeleteLink with the existing shortcut's absolute path. CreateShortcut provides this path by appending the value in strAllUserProgram, the value in strFolderName, and the g_arrShortCutParms(1) value (i.e., the shortcut name).

    After CreateShortcut provides the necessary information, CreateLink, CreateURLLink, and DeleteLink perform their respective tasks. CreateLink and CreateURLLink use the WSH Shell object's CreateShortcut method to create a shortcut. CreateLink and CreateURLLink then use the parameters that CreateShortcut passed to them to fill the shortcut's various properties. DeleteLink uses the FileSystemObject's DeleteFile method to delete the existing shortcut.

    After CreateLink, CreateURLLink, and DeleteLink create or delete a shortcut, CreateShortcut adds information to the log file. CreateShortcut adds either Created Shortcut: or Deleted Shortcut:, followed by the shortcut's name.

    A Useful Tool to Manage Desktop Shortcuts
    Together, the .ini file and SWDist.vbs provide a useful tool. With this tool, you can quickly and effectively create and update your users' desktop shortcuts to various applications and resources.