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: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, 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.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, 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.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, 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.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, 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

VBSTART 
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
*/
Last edited by Grovkillen on Wed May 17, 2023 3:42 pm, edited 2 times in total.
Let>ME=%Script%

Running: 15.0.27
version history

OldGeek
Junior Coder
Posts: 29
Joined: Thu Oct 30, 2014 11:49 am
Location: Upstate NY, USA

Re: VB code to create a link file

Post by OldGeek » Tue May 16, 2023 1:33 pm

As a beginner I'm always looking for tips and code samples. I think when you cut / paste
you missed the VBStart at the start. Just my two cents in case some other beginner is
looking for examples. Thanks for the code sample.
MS v15 | Windows 11 Pro | NY, USA

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: VB code to create a link file

Post by Grovkillen » Wed May 17, 2023 3:42 pm

Thanks for pointing it out, fixed now.
Let>ME=%Script%

Running: 15.0.27
version history

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