24

how to create a shortcut for a exe from a batch file.

i tried

call link.bat "c:\program Files\App1\program1.exe" "C:\Documents and Settings\%USERNAME%\Desktop" "C:\Documents and Settings\%USERNAME%\Start Menu\Programs" "Program1 shortcut"

but it did not worked.

link.bat can be found at http://www.robvanderwoude.com/amb_shortcuts.html

3
  • 6
    Please don't assume that Program Files is at "C:\Program Files". (%PROGRAMFILES% can work, though). Similarly, don't assume that the user's home directory is in "C:\Documents and Settings\%USERNAME%" (It's %HOMEDIR%). And Desktop may not be called Desktop.
    – Jay Bazuzi
    Feb 12, 2009 at 17:26
  • Is there a solution available for creating shortcuts on Windows 7? Jan 3, 2013 at 8:05
  • 3
    @JayBazuzi it is %HomePath% on Windows 7, 8, & Server 2012. :)
    – kodybrown
    Oct 28, 2013 at 18:02

9 Answers 9

22

Your link points to a Windows 95/98 version and I guess you have at least Windows 2000 or XP. You should try the NT version here.

Alternatively use a little VBScript that you can call from the command line:

set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")

' command line arguments
' TODO: error checking
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)

set objSC = objWSHShell.CreateShortcut(sShortcut) 

objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory

objSC.Save

Save the file as createLink.vbs and call it like this to get what you originally tried:

cscript createLink.vbs "C:\Documents and Settings\%USERNAME%\Desktop\Program1 shortcut.lnk" "c:\program Files\App1\program1.exe" 
cscript createLink.vbs "C:\Documents and Settings\%USERNAME%\Start Menu\Programs\Program1 shortcut.lnk" "c:\program Files\App1\program1.exe" 

That said I urge you not to use hardcoded paths like "Start Menu" since they're different in localized versions of windows. Modify the script instead to use special folders.

4
  • 1
    I added a couple of lines to set the WorkingDirectory properly if the target is an exe - without that, any exe that expects to be launched from it's own directory will fail, usually with a meaningless error message.
    – Mason
    Jun 20, 2012 at 20:01
  • @Mason: Could you post your improvements? Would really help me.
    – aRestless
    Jul 12, 2012 at 8:51
  • 1
    @aRestless: Sorry, I submitted an edit and it was apparently rejected. The mod is to add the following right before objSC.Save: If InStr(sTargetPath, ".exe") > 0 Then objSC.WorkingDirectory = Left(sTargetPath, InStrRev(sTargetPath, "\")) End If
    – Mason
    Jul 12, 2012 at 21:24
  • 1
    @aRestless: As suggested, and since Mason can't edit the post, I updated the script to set the working directory.
    – VVS
    Jul 20, 2012 at 10:10
12

This is the kind of thing that PowerShell is really good at, and is therefore a reason to eschew batch files and get on PowerShell the bandwagon.

PowerShell can talk to .NET. For example, you can get the location of the Desktop like this:

[Environment]::GetFolderPath("Desktop")

PowerShell can talk to COM objects, including WScript.Shell, which can create shortcuts:

New-Object -ComObject WScript.Shell).CreateShortcut( ... )

So your script might look like:

$linkPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "MyShortcut.lnk"
$targetPath = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "MyCompany\MyProgram.exe"
$link = (New-Object -ComObject WScript.Shell).CreateShortcut( $linkpath )
$link.TargetPath = $targetPath
$link.Save()

Shortcuts have a lot of settings that WScript.Shell can't manipulate, like the "run as administrator" option. These are only accessible through the Win32 interface IShellLinkDataList, which is a real pain to use, but it can be done.

9

Using vbscript:

set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("AllUsersDesktop" )
set oShellLink = WshShell.CreateShortcut(strDesktop & "\shortcut name.lnk" )
oShellLink.TargetPath = "c:\application folder\application.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "c:\application folder\application.ico"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = "c:\application folder"
oShellLink.Save 

Ref: http://www.tomshardware.com/forum/52871-45-creating-desktop-shortcuts-command-line

Failing that, a quick google search shows there's a number of third party tools that can create .lnk files for application shortcuts. I'm assuming you need to stick to stuff that's available natively on Windows though? VBscript is probably your best bet, otherwise I'd suggest trying copying the .lnk file from your machine or using it as a sample to see the correct format for a shortcut file.

3

On XP I wrote makeshortcut.vbs

Set oWS = WScript.CreateObject("WScript.Shell")
If wscript.arguments.count < 4 then
  WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir "
  WScript.Quit
end If
shortcutPath = wscript.arguments(0) & ".LNK"
targetPath = wscript.arguments(1)
arguments = wscript.arguments(2)
workingDir = wscript.arguments(3)

WScript.Echo "Creating shortcut " & shortcutPath & " targetPath=" & targetPath & " arguments=" & arguments & " workingDir=" & workingDir

Set oLink = oWS.CreateShortcut(shortcutPath) 
oLink.TargetPath = targetPath
oLink.Arguments = arguments
' oLink.Description = "MyProgram"
' oLink.HotKey = "ALT+CTRL+F"
' oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
' oLink.WindowStyle = "1"
oLink.WorkingDirectory = workingDir
oLink.Save

It takes exactly 4 args, so it could be improved by making the later 2 optional.I only post because it echo's usage, which might be useful to some. I like WS's soln using special folders and ExpandEnvironmentStrings

1

Additional note: the link.bat you're using is for Windows 95/98 only:

This batch file is for Windows 95/98 only. I will post the NT equivalent in the NT newsgroup soon.

NT version is posted at http://www.robvanderwoude.com/amb_shortcutsnt.html instead. You might try that for a .bat approach if preferred over vbscript.

1

Alternative method, using a third party utility:

Creating a Shortcut from the command line (batch file)

XXMKLINK:

With XXMKLINK, you can write a batch file for software installation which has been done by specialized installation programs. Basically, XXMKLINK is a tool that gathers various information from a command line and packages it into a shortcut.

xxmklink spath opath 

where 

  spath     path of the shortcut (.lnk added as needed)
  opath     path of the object represented by the shortcut
0
1

You can check shortcutjs.bat:

::creates a shortcut that will start the target with minimized window and admin permissions
shortcutjs.bat -linkfile myscriptMin.lnk -target "%cd%\myscript.bat" -windowstyle 7 -adminpermissions yes

::creates a shortcut with a hot keys
shortcutjs.bat -linkfile myscriptHK.lnk -target "%cd%\myscript.bat" -hotkey "ALT+CTRL+P"

With this you can also edit existing shortcut or only display its properties.

0

This worked for me on Windows XP ms-dos, I still haven't tried it on Windows 7. It's just like creating a symbolic link in Linux.

shortcut -T source.exe destination.lnk
3
  • @henginy did you try the command? This is done using "Windows Script Host". Mar 21, 2013 at 19:36
  • Of course, but I've tried it in command prompt since it's not specified otherwise. I'll try in WS Host also.
    – henginy
    Mar 22, 2013 at 10:46
  • Windows 95 support tools and NT 4 Server Resource Kit contained a shortcut.exe tool. There is also a freeware shortcut.exe from Optimum X.
    – Mofi
    Oct 4, 2014 at 19:10
0

In the end I decided to write the correct script, because no solution works for me You will need two fileLocal Settings\ first

createSCUT.bat

@echo on
set VBS=createSCUT.vbs 
set SRC_LNK="shortcut1.lnk"
set ARG1_APPLCT="C:\Program Files\Google\Chrome\Application\chrome.exe"
set ARG2_APPARG="--profile-directory=QuteQProfile 25QuteQ"
set ARG3_WRKDRC="C:\Program Files\Google\Chrome\Application"
set ARG4_ICOLCT="%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Profile 25\Google Profile.ico"
cscript %VBS% %SRC_LNK% %ARG1_APPLCT% %ARG2_APPARG% %ARG3_WRKDRC% %ARG4_ICOLCT%

and second

createSCUT.vbs

Set objWSHShell = WScript.CreateObject("WScript.Shell")
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")
If WScript.arguments.count = 5 then
    WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir IconLocation"
    sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    set objSC = objWSHShell.CreateShortcut(sShortcut) 
    sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
    sArguments = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
    sWorkingDirectory = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(3))
    sIconLocation = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(4))
    objSC.TargetPath = sTargetPath
    rem http://www.bigresource.com/VB-simple-replace-function-5bAN30qRDU.html#
    objSC.Arguments = Replace(sArguments, "QuteQ", Chr(34))
    rem http://msdn.microsoft.com/en-us/library/f63200h0(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/267k4fw5(v=vs.90).aspx
    objSC.WorkingDirectory = sWorkingDirectory
    objSC.Description = "Love Peace Bliss"
    rem 1 restore 3 max 7 min
    objSC.WindowStyle = "3"
    rem objSC.Hotkey = "Ctrl+Alt+e";
    objSC.IconLocation = sIconLocation
    objSC.Save
    WScript.Quit
end If
If WScript.arguments.count = 4 then
    WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir "

    sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    set objSC = objWSHShell.CreateShortcut(sShortcut) 
    sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
    sArguments = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
    sWorkingDirectory = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(3))
    objSC.TargetPath = sTargetPath
    objSC.Arguments = Replace(sArguments, "QuteQ", Chr(34))
    objSC.WorkingDirectory = sWorkingDirectory
    objSC.Description = "Love Peace Bliss"
    objSC.WindowStyle = "3"
    objSC.Save
    WScript.Quit
end If
If WScript.arguments.count = 2 then
    WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath"
    sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
    set objSC = objWSHShell.CreateShortcut(sShortcut) 
    sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
    sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)
    objSC.TargetPath = sTargetPath
    objSC.WorkingDirectory = sWorkingDirectory
    objSC.Save
    WScript.Quit
end If

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.