The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,878 other followers

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:

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

        Dim StdOut : Set StdOut = WScript.StdOut
        StdOut.WriteLine "TargetPath does not exist: " & TargetPath
    End If
End Function


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: