creating a shortcut for a exe from a batch file
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.
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.
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
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.