creating a shortcut for a exe from a batch file

后端 未结 9 1488
清酒与你
清酒与你 2020-11-27 05:39

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\\         


        
相关标签:
9条回答
  • 2020-11-27 06:17

    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

    0 讨论(0)
  • 2020-11-27 06:17

    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 讨论(0)
  • 2020-11-27 06:22

    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.

    0 讨论(0)
  • 2020-11-27 06:23

    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
    
    0 讨论(0)
  • 2020-11-27 06:26

    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
    
    0 讨论(0)
  • 2020-11-27 06:29

    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.

    0 讨论(0)
提交回复
热议问题