Try the Code – Ask Questions – Ask me what you need.
Today we are going to talk about making shortcuts to any file or folder. The code is as follows:
Option Explicit Dim obj Set obj = CreateObject ("wscript.shell") obj.CreateShortcut("C:\Users\rock\Desktop\newshortcut.lnk")
Shortcuts have the extension .lnk, which stands for links. It does not matter whether the shortcut is for a folder or file.
So far, we have 1. the place where the shortcut is created, 2. the name of the shortcut which is newshortcut.lnk. Now we need to specify the file or folder we are going to make a shortcut for. So we are going to store/set the CreateObject (“wscript.shell”) as a variable. Like so:
Option Explicit Dim obj, Link Set obj = CreateObject ("wscript.shell") Set Link = obj.CreateShortcut("C:\Users\rock\Desktop\newshortcut.lnk")
Continuing, to change our shortcut to anything we want:
Option Explicit Dim obj, Link Set obj = CreateObject ("wscript.shell") Set Link = obj.CreateShortcut("C:\Users\rock\Desktop\newshortcut.lnk") Link.TargetPath = ""
TargetPath will choose what VbScript makes a shortcut for. ie. if I have a MP3 file in the downloads folder that I want to make a shortcut for:
Option Explicit Dim obj, Link Set obj = CreateObject ("wscript.shell") Set Link = obj.CreateShortcut("C:\Users\rock\Desktop\newshortcut.lnk") Link.TargetPath = "C:\Users\rock\Downloads\Crash.MP3" Link.Save 'You need to save the shortcut, otherwise the shortcut will not appear where it is designated. In this case, the desktop.
Output: