Creating Lnk ShortCut files the programmatic way
Posted by jpluimers on 2013/05/01
A while ago, I needed to automatically create a bunch of shortcuts all in the same directory, and all the batch files in a different directory.
There’s different kinds of doing this:
- Shell Links using the IShellLink interface
- CreateShortcut Method in the Shell namespace of the Windows Scripting Host
- Function FileCreateShortcut in AutoIt
I needed a one-off, so I came up with some code like this.
Call CreateLink("Huey.lnk", "Huey.bat", "Huey")
Call CreateLink("Dewey.lnk", "Dewey.bat", "Dewey")
Call CreateLink("Louie.lnk", "Louie.bat", "Louie")
'*****************************************************************************
'* Function CreateLink ( )
'*******************
'* Purpose : Creates one Lnk ShortCut file
'* Parameters : LinkFileName, BatchFileName, Description
'* Return code : none
'*****************************************************************************
Function CreateLink(ByVal LinkFileName, ByVal BatchFileName, ByVal Description)
Dim WorkingDirectory : WorkingDirectory = "C:\Duck\"
Dim IconLocation : IconLocation = WorkingDirectory & "Huey_Dewey_Louie.ico"
Dim ShortcutDirectory : ShortcutDirectory = WorkingDirectory & "ShortCuts\"
Dim LinkPath : LinkPath = ShortcutDirectory & LinkFileName
Dim TargetPath : TargetPath = WorkingDirectory & BatchFileName
Dim FileSystemObject : Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
If FileSystemObject.FileExists(TargetPath) Then
'make a shortcut
Dim Shell : Set Shell = CreateObject("WScript.Shell")
Dim LnkShortCut : Set LnkShortCut = Shell.CreateShortcut(LinkPath)
'LnkShortCut.HotKey = "CTRL+ALT+SHIFT+E"
LnkShortCut.WindowStyle = 1
LnkShortCut.WorkingDirectory = WorkingDirectory
LnkShortCut.IconLocation = IconLocation
LnkShortCut.Description = Description
LnkShortCut.TargetPath = TargetPath
LnkShortCut.Save
Else
Dim StdOut : Set StdOut = WScript.StdOut
StdOut.WriteLine "TargetPath does not exist: " & TargetPath
End If
End Function
–jeroen






Leave a comment