VB code to create a link file

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 665
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

VB code to create a link file

Post by Grovkillen » Thu Nov 03, 2016 2:47 pm

Hi there,

I just thought I may share my findings regarding creating a short cut file within MS. I do this to create a link in the startup folder (this example I pointed it to the desktop).

Code: Select all

VBSTART
Function GetFolder(LinkFolder, TargetPath, IconPath, LinkName)
  Set WShell = CreateObject("WScript.Shell")
	GetFolder = WShell.SpecialFolders(LinkFolder)
  Set oShellLink = CreateObject("WScript.Shell").CreateShortcut(GetFolder & "\" & LinkName & ".lnk")
  oShellLink.WorkingDirectory = GetFolder
  oShellLink.TargetPath = TargetPath
  oShellLink.IconLocation = IconPath
  oShellLink.Description = TargetPath
  oShellLink.Save
	Set WShell = Nothing
  Set oShellLink = Nothing
End Function
VBEND

Let>LINK_FOLDER=Desktop
Let>LINK_TARGET=C:\
Let>ICON_PATH=C:\*****.ico
Let>LINK_NAME=Shortcut
VBEval>GetFolder("%LINK_FOLDER%", "%LINK_TARGET%", "%ICON_PATH%", "%LINK_NAME%"),FOLDER_PATH
MDL>FOLDER_PATH
Enjoy! :)

More information is found here>>

Special folders to use>>
Let>ME=%Script%

Running: 15.0.18
version history

User avatar
Grovkillen
Automation Wizard
Posts: 665
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: VB code to create a link file

Post by Grovkillen » Fri Nov 04, 2016 11:43 am

List of useful places to create a shortcut file:

LINK_FOLDER=

"Startup" (Will create a link in the startup folder for the user, thus have your script or whatever started on Windows login")
"Programs" (Will create a link on the users start menu, under programs)
"Desktop" (Will create a link on the users desktop)
Let>ME=%Script%

Running: 15.0.18
version history

User avatar
Grovkillen
Automation Wizard
Posts: 665
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: VB code to create a link file

Post by Grovkillen » Sun Dec 31, 2017 1:50 pm

Note to myself. Font folder is found by using this code:

Code: Select all

VBSTART
Function GetFolder(LinkFolder)
  Set WShell = CreateObject("WScript.Shell")
   GetFolder = WShell.SpecialFolders(LinkFolder)
   Set WShell = Nothing
End Function
VBEND
Let>LINK_FOLDER=Fonts
VBEval>GetFolder("%LINK_FOLDER%"),FOLDER_PATH
MDL>FOLDER_PATH
Let>ME=%Script%

Running: 15.0.18
version history

User avatar
Grovkillen
Automation Wizard
Posts: 665
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: VB code to create a link file

Post by Grovkillen » Thu Jun 10, 2021 10:18 am

An updated version:

Code: Select all

Function LinkFile(LinkFolder, TargetPath, LinkName, IconPath)
  Set WShell = CreateObject("WScript.Shell")
  LinkFile = WShell.SpecialFolders(LinkFolder)
  Set oShellLink = CreateObject("WScript.Shell").CreateShortcut(LinkFile & "\" & LinkName & ".lnk")
  oShellLink.WorkingDirectory = LinkFile
  oShellLink.TargetPath = TargetPath
  If IconPath <> "" Then
    oShellLink.IconLocation = IconPath
  End if
  oShellLink.Description = TargetPath
  oShellLink.Save
	Set WShell = Nothing
  Set oShellLink = Nothing
End Function
VBEND

/*
Let>LINK_FOLDER=Startup
Let>LINK_TARGET=C:\your_file.exe
Let>ICON_PATH=C:\*****.ico   <---- leave as "" if not wanted
Let>LINK_NAME=Shortcut
VBEval>LinkFile("%LINK_FOLDER%", "%LINK_TARGET%", "%LINK_NAME%", "%ICON_PATH%"),FOLDER_PATH
//or
VBEval>LinkFile("%LINK_FOLDER%", "%LINK_TARGET%", "%LINK_NAME%", ""),FOLDER_PATH
*/
Let>ME=%Script%

Running: 15.0.18
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts