How do I create a shortcut via command-line in Windows?

后端 未结 9 782
忘掉有多难
忘掉有多难 2020-11-28 04:17

I want my .bat script (test.bat) to create a shortcut to itself so that I can copy it to my windows 8 Startup folder.

I have written this line of code to copy the f

相关标签:
9条回答
  • 2020-11-28 05:12

    The best way is to run this batch file. open notepad and type:-

    @echo off
    echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
    echo sLinkFile = "GIVETHEPATHOFLINK.lnk" >> CreateShortcut.vbs
    echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
    echo oLink.TargetPath = "GIVETHEPATHOFTARGETFILEYOUWANTTHESHORTCUT" >> CreateShortcut.vbs
    echo oLink.Save >> CreateShortcut.vbs
    cscript CreateShortcut.vbs
    del CreateShortcut.vbs
    

    Save as filename.bat(be careful while saving select all file types) worked well in win XP.

    0 讨论(0)
  • 2020-11-28 05:13

    I would like to propose different solution which wasn't mentioned here which is using .URL files:

    set SHRT_LOCA=%userprofile%\Desktop\new_shortcut2.url
    set SHRT_DEST=C:\Windows\write.exe
    echo [InternetShortcut]> %SHRT_LOCA%
    echo URL=file:///%SHRT_DEST%>> %SHRT_LOCA%
    echo IconFile=%SHRT_DEST%>> %SHRT_LOCA%
    echo IconIndex=0>> %SHRT_LOCA%
    

    Notes:

    • By default .url files are intended to open web pages but they are working fine for any properly constructed URI
    • Microsoft Windows does not display the .url file extension even if "Hide extensions for known file types" option in Windows Explorer is disabled
    • IconFile and IconIndex are optional
    • For reference you can check An Unofficial Guide to the URL File Format of Edward Blake
    0 讨论(0)
  • 2020-11-28 05:17

    Rohit Sahu's answer worked best for me in Windows 10. The PowerShell solution ran, but no shortcut appeared. The JScript solution gave me syntax errors. I didn't try mklink, since I didn't want to mess with permissions.

    I wanted the shortcut to appear on the desktop. But I also needed to set the icon, the description, and the working directory. Note that MyApp48.bmp is a 48x48 pixel image. Here's my mod of Rohit's solution:

    @echo off
    cd c:\MyApp
    echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
    echo sLinkFile = "%userprofile%\Desktop\MyApp.lnk" >> CreateShortcut.vbs
    echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
    echo oLink.TargetPath = "C:\MyApp\MyApp.bat" >> CreateShortcut.vbs
    echo oLink.WorkingDirectory = "C:\MyApp" >> CreateShortcut.vbs
    echo oLink.Description = "My Application" >> CreateShortcut.vbs
    echo oLink.IconLocation = "C:\MyApp\MyApp48.bmp" >> CreateShortcut.vbs
    echo oLink.Save >> CreateShortcut.vbs
    cscript CreateShortcut.vbs
    del CreateShortcut.vbs
    
    0 讨论(0)
提交回复
热议问题