Create a shortcut to current folder on user's desktop

15,767

Solution 1

The best way finally seems to be a VBS Script. Here is what I finally got working right:

Option Explicit
On Error Resume Next

Private WshShell
Private strDesktop
Private oShellLink
Private aSplit

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
aSplit = Split(WScript.ScriptFullName, "\")

set oShellLink = WshShell.CreateShortcut(strDesktop & "\" & aSplit(Ubound(aSplit) - 1) & ".lnk")
oShellLink.TargetPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
oShellLink.WindowStyle = 1
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
oShellLink.Save 

MsgBox "Shortcut to " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & " added yo your desktop!"

Solution 2

Great code! Out of curiosity, since this works for the directory the script is currently in, do you have a way to get it to show up in every directory? Otherwise, it doesn't seem like there's much difference between learning this and learning to make a shortcut the native way. You would still have to drag and drop the script into the current folder, wouldn't you?

While stumbling toward a solution, I got as far as letting users navigate to and select a particular file they need to link to. I don't know if you would have any use for that.

Dim diaSelectFile
    Set diaSelectFile = Application.FileDialog(msoFileDialogFilePicker)

diaSelectFile.Show

strPickedFile = diaSelectFile.SelectedItems(1)

Set diaSelectFile = Nothing

Dim oWsh
Dim myshortcut
Dim oShortcut
Dim strSplitFileName
Dim strTarget
Dim nShortName

Set oWsh = CreateObject("WScript.Shell")

strSplitFileName = Split(strPickedFile, "\")
nShortName = UBound(strSplitFileName)
strTarget = strSplitFileName(nShortName)

myshortcut = "C:\users\%USERNAME%\Desktop\" & strTarget & " - Shortcut" & ".lnk"

Set oShortcut = oWsh.CreateShortcut(myshortcut)

With oShortcut
    .TargetPath = strPickedFile
    .Save
End With

Set oWsh = Nothing
Set oShortcut = Nothing

Again, though, this feels more complex than right-clicking and sending a shortcut to the desktop. Who are the users that need this? I know I've had austistic friends who struggle with what we might consider basic tasks on the computer. I'd definitely be interested to know if the script you came up with actually helps your clientele.

Share:
15,767
dan
Author by

dan

Updated on June 04, 2022

Comments

  • dan
    dan almost 2 years

    I would like to automatically create a shortcut to the current's folder on the user's desktop. Some users I'm working with don't know how to create shortcuts or how to drag and drop a folder. I just want to create a file named "CLICK ME TO CREATE A SHORTCUT TO THIS FOLDER ON YOUR DESKTOP" that will work in any folder I want.

    For example, if I run C:\myRandomFolder\CLICK ME.whatever, I want it to create a shortcut to "C:\myRandomFolder\" named "myRandomFolder" on "D:\Documents and Settings\%username%\Desktop".

    I'm wondering if I'm better using a batch file (.bat), VB Script (.vbs) or any other scripting language to do so. What would be the easiest and better way of doing it?

  • dan
    dan over 11 years
    Thank you for the input. At my office, most of the people are typing with 2 fingers (if you see what I mean). The secretary is however just a little bit better, enough to cut/paste the VBS into the right folder. She just has to send a direct link to the VBS by email to all employees with some instructions and the shortcut will be automatically added to their desktop.
  • dan
    dan over 11 years
    As for showing up the VBS in all (sub)directories, it would be easy to copy the scriptin each subdirectories and name it _shortcut.vbs. There could be a _shortcutGen.vbs on the root that generates the current _shortcut.vbs and duplicates it into all subfolders.