Creating Windows and Internet Shortcuts in Scripts
Include
the following
subroutine in your script to allow easy creation of
Internet Shortcuts
(*.url
) and Windows Shortcuts
(*.lnk
):
Sub Shortcut(LinkFile, CommandLine) Set WshShell = WScript.CreateObject("WScript.Shell") If LCase(Right(LinkFile, 4)) <> ".lnk" And _ LCase(Right(LinkFile, 4)) <>".url" Then _ LinkFile = LinkFile & ".LNK" Set ShortcutHandle = WshShell.CreateShortcut(LinkFile) ShortcutHandle.TargetPath = CommandLine ShortcutHandle.Save End Sub
To create a shortcut to a program or file, use the following statement:
Call Shortcut("c:\Windows\sendto\Notepad.lnk", _ "Notepad.exe")
To create a shortcut to an Internet address:
Call Shortcut("c:\Windows\desktop\Annoyances.url", _ "http://www.annoyances.org/")
If the first parameter, LinkFile
, ends in
.LNK
(case
doesn’t matter), the Shortcut
subroutine
will automatically create a standard Windows shortcut; if LinkFile
ends in .URL
, however, an Internet Shortcut file
will be created. Note the If...Then
structure in
the routine, which automatically adds .LNK
to
any shortcut filenames that aren’t properly named.
If you specify a nonexistent folder in the path for the new shortcut
file, an “Unspecified Error” will occur. You may want to
use the FolderExists
function, detailed in the
Section 9.4 topic earlier in this
chapter, to eliminate the possibility of this error.
Get Windows Me Annoyances now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.