tags:

views:

4358

answers:

5

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

A: 

Alternative method, using a third party utility:

Creating a Shortcut from the command line (batch file)

Martin
+4  A: 

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.

Jay
+5  A: 

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")

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

set objSC = objWSHShell.CreateShortcut(sShortcut) 

objSC.TargetPath = sTargetPath

objSC.Save

Save the file as createLink.vbs all 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.

VVS
A: 

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.

Jay
+2  A: 

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.

Jay Bazuzi