Search code examples
vbabatch-filevbscriptprogramming-languagesshortcut

Create a shortcut to current folder on user's desktop


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?


Solution

  • 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!"