4.6. Creating a Shortcut

Problem

You want to create a shortcut to a file or folder. A shortcut is simply a file with a .lnk extension that redirects you to another file or folder when clicked on in Windows Explorer. You can also distinguish shortcut files from regular files by a small arrow in the bottom left side of their icons.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. Browse to the file or folder you want to create a shortcut for.

  3. Right-click the file or folder and select Create Shortcut.

  4. Move the shortcut file to desired location.

Using a command-line interface

The Windows NT Resource Kit had a tool called shortcut.exe that could be used to create shortcuts, but it isn't present in the Windows 2000 or Windows Server 2003 Resource Kits. The MKS Toolkit (http://www.mkssoftware.com/products/tk/), an excellent product that provides numerous Unix-based utilities for the Windows platform, contains a shortcut.exe tool, which can create shortcuts. Here is the syntax for that tool:

> shortcut [-f dest-file] [-a arglist] [-w workdir] [-s show-keyword] 
[-i iconpath[,iconindex]] [-d description] [-D] shortcut-file
   
-a arglist  
defines any arguments to the executable file specified with the -f dest-file option. 
   
-d description  
specifies descriptive text to be embedded in the link file. description is only
displayed when you use the -p option to print the contents of the link file. If
description includes space, the text should be enclosed in double quotes ("). 
   
-D shortcut-file  
specifies the shortcut-file is on the desktop. 
   
-f dest-file  
specifies the full path and file name of the executable file to be run when the link
file is double-clicked. 
   
-i iconpath[,iconindex]  
specifies the icon to be displayed for the link file. If the specified icon contains
multiple images, determine which image is to be displayed by entering the appropriate
number for iconindex. 
   
-p  
displays the contents of the specified shortcut file. 
   
-s show-keyword  
specifies how the executable is displayed when invoked. show-keyword can be one
of the following: 
   
SW_SHOW             starts the program in standard mode
SW_SHOWMAXIMIZED    starts the program in full screen mode
SW_SHOWMINIMIZED    starts the program minimized
SW_SHOWMINNOACTIVE  displays the program as an icon but does not start it
   
When this option is not specified, shortcut defaults to SW_SHOW. 
   
-w workdir  
specifies the working directory in which the program is started.

Here is an example:

> shortcut -f c:/perl/bin/perl.exe -a -L perl-link.lnk

Tip

The MKS Toolkit isn't free and can be quite expensive, but there is another shortcut utility from this site that is free: http://www.optimumx.com.

Using VBScript

' This code creates a 
shortcut.
   
set objWSHShell = CreateObject("WScript.Shell")
   
' Pass the path to the shortcut
set objSC = objWSHShell.CreateShortcut("d:\mylog.lnk") 
   
' Description - Description of the shortcut
objSC.Description = "Shortcut to MyLog file"
   
' HotKey - hot key sequence to launch the shortcut
objSC.HotKey = "CTRL+ALT+SHIFT+X"
   
' IconLocation - Path of icon to use for the shortcut file
objSC.IconLocation = "notepad.exe, 0"  ' 0 is the index
   
' TargetPath = Path to source file or folder
objSC.TargetPath = "c:\windows\notepad.exe"
   
' Arguments - Any additional parameters to pass to TargetPath
objSC.Arguments = "c:\mylog.txt"
   
' WindowStyle - Type of window to create
objSC.WindowStyle = 1   ' 1 = normal; 3 = maximize window; 7 = minimize
   
' WorkingDirectory - Location of the working directory for the source app
objSC.WorkingDirectory = "c:\"
objSC.Save
WScript.Echo "Shortcut to mylog created"
   
' This code finds all shortcuts on a system.
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colSCs = objWMI.InstancesOf("Win32_ShortcutFile")
for each objSC in colSCs
    WScript.Echo "Name:   " & objSC.Name
    WScript.Echo "Target: " & objSC.Target
    WScript.Echo
    intCount = intCount + 1
next
WScript.Echo "Total shortcuts: " & intCount

Discussion

Shortcuts can be useful to quickly access files and folders that are distributed across the filesystem or on remote servers. The problem with shortcuts is that they can quickly become out of date if not maintained. There is a tool in the Resource Kit to help identify dead shortcuts called chklnks.exe. It searches for all shortcuts whose target does not exist and displays them in a list. You can right-click on a shortcut to see the missing target location or delete selected dead shortcuts.

See Also

MS KB 140443 (How to Create a Shortcut on the Desktop)

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.