如何在 Windows 中通过命令行创建快捷方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30028709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:03:20  来源:igfitidea点击:

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

windowsbatch-filewindows-8cmdshortcut

提问by Maslor

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.

我希望我的 .bat 脚本 (test.bat) 为自身创建一个快捷方式,以便我可以将其复制到我的 Windows 8 启动文件夹中。

I have written this line of code to copy the file but I haven't yet found a way to create the said shortcut, as you can see it only copies the script.

我已经编写了这行代码来复制文件,但我还没有找到创建所述快捷方式的方法,因为您可以看到它只复制脚本。

xcopy "C:\Users\Gabriel\Desktop\test.bat" "C:\Users\Gabriel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

Can you help me out?

你能帮我吗?

回答by rojo

You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0in %userprofile%\Start Menu\Programs\Startup:

您可以使用 PowerShell 命令。将此粘贴到您的批处理脚本中,它将创建一个快捷方式%~f0in %userprofile%\Start Menu\Programs\Startup

powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Start Menu\Programs\Startup\%~n0.lnk');$s.TargetPath='%~f0';$s.Save()"


If you prefer not to use PowerShell, you could use mklinkto make a symbolic link. Syntax:

如果您不想使用 PowerShell,则可以使用mklink来创建符号链接。句法:

mklink saveShortcutAs targetOfShortcut

See mklink /?in a console window for full syntax, and this web pagefor further information.

请参阅mklink /?在完整的语法控制台窗口,并且该网页以获取更多信息。

In your batch script, do:

在您的批处理脚本中,执行以下操作:

mklink "%userprofile%\Start Menu\Programs\Startup\%~nx0" "%~f0"

The shortcut created isn't a traditional .lnk file, but it should work the same nevertheless. Be advised that this will only work if the .bat file is run from the same drive as your startup folder. Also, apparently admin rights are required to create symbolic links.

创建的快捷方式不是传统的 .lnk 文件,但它应该可以正常工作。请注意,这仅在 .bat 文件与启动文件夹从同一驱动器运行时才有效。此外,显然需要管理员权限才能创建符号链接。

回答by npocmaka

Cannot be done with pure batch.Check the shortcutJS.bat- it is a jscript/bathybrid and should be used with .batextension:

不能用纯批处理来完成。检查shortcutJS.bat- 它是一个jscript/bat混合体,应该与.bat扩展一起使用:

call shortcutJS.bat -linkfile "%~n0.lnk" -target  "%~f0" -linkarguments "some arguments"

With -helpyou can check the other options (you can set icon , admin permissions and etc.)

有了-help你可以检查其他选项(可以设置图标,管理权限等)

回答by DrFractal

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.

Rohit Sahu 的回答在 Windows 10 中最适合我。PowerShell 解决方案运行了,但没有出现快捷方式。JScript 解决方案给了我语法错误。我没有尝试 mklink,因为我不想弄乱权限。

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:

我希望快捷方式出现在桌面上。但我还需要设置图标、描述和工作目录。请注意,MyApp48.bmp 是一个 48x48 像素的图像。这是我的 Rohit 解决方案模型:

@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

回答by Rohit Sahu

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.

另存为 filename.bat(保存选择所有文件类型时要小心)在 win XP 中运行良好。

回答by CodeFu

link.vbs

链接.vbs

set fs  = CreateObject("Scripting.FileSystemObject")
set ws  = WScript.CreateObject("WScript.Shell")
set arg = Wscript.Arguments

linkFile = arg(0)

set link = ws.CreateShortcut(linkFile)
    link.TargetPath = fs.BuildPath(ws.CurrentDirectory, arg(1))
    link.Save

command

命令

C:\dir>link.vbs ..\shortcut.txt.lnk target.txt

回答by Matt Borcherding

Nirsoft's NirCMD can create shortcuts from a command line, too. (Along with a pile of other functions.) Free and available here:

Nirsoft 的 NirCMD 也可以从命令行创建快捷方式。(以及一堆其他功能。)在这里免费且可用:

http://www.nirsoft.net/utils/nircmd.html

http://www.nirsoft.net/utils/nircmd.html

Full instructions here: http://www.nirsoft.net/utils/nircmd2.html#using(Scroll down to the "shortcut" section.)

完整说明在这里:http: //www.nirsoft.net/utils/nircmd2.html#using(向下滚动到“快捷方式”部分。)

Yes, using nircmd does mean you are using another 3rd-party .exe, but it can do some functions not in (most of) the above solutions (e.g., pick a icon # in a dll with multiple icons, assign a hot-key, and set the shortcut target to be minimized or maximized).

是的,使用 nircmd 确实意味着您正在使用另一个 3rd-party .exe,但它可以执行上述解决方案(大多数)中没有的一些功能(例如,在具有多个图标的 dll 中选择一个图标 #,分配一个热键,并将快捷方式目标设置为最小化或最大化)。

Though it appears that the shortcutjs.bat solution above can do most of that, too, but you'll need to dig more to find how to properly assign those settings. Nircmd is probably simpler.

尽管上面的shortcutjs.bat 解决方案似乎也可以完成大部分工作,但您需要深入了解如何正确分配这些设置。Nircmd 可能更简单。

回答by n3vermind

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

我想提出这里没有提到的使用.URL文件的不同解决方案:

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 .urlfiles are intended to open web pages but they are working fine for any properly constructed URI
  • Microsoft Windows does not display the .urlfile extension even if "Hide extensions for known file types"option in Windows Explorer is disabled
  • IconFileand IconIndexare optional
  • For reference you can check An Unofficial Guide to the URL File Formatof Edward Blake
  • 默认情况下,.url文件旨在打开网页,但它们适用于任何正确构造的 URI
  • .url即使Windows 资源管理器中的“隐藏已知文件类型的扩展名”选项被禁用,Microsoft Windows 也不会显示文件扩展名
  • IconFile并且IconIndex是可选的
  • 作为参考,您可以查看Edward Blake的 URL 文件格式非官方指南

回答by Hackoo

I present a small hybrid script [BAT/VBS]to create a desktop shortcut. And you can of course modifie it to your purpose.

我展示了一个小的混合脚本[BAT/VBS]来创建桌面快捷方式。您当然可以根据自己的目的对其进行修改。

@echo off
mode con cols=87 lines=5 & color 9B
Title Shortcut Creator for your batch and applications files by Hackoo 2015
Set MyFile=%~f0
Set ShorcutName=HackooTest
(
echo Call Shortcut("%MyFile%","%ShorcutName%"^)
echo ^'**********************************************************************************************^)
echo Sub Shortcut(ApplicationPath,Nom^)
echo    Dim objShell,DesktopPath,objShortCut,MyTab
echo    Set objShell = CreateObject("WScript.Shell"^)
echo    MyTab = Split(ApplicationPath,"\"^)
echo    If Nom = "" Then
echo    Nom = MyTab(UBound(MyTab^)^)
echo    End if
echo    DesktopPath = objShell.SpecialFolders("Desktop"^)
echo    Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& Nom ^& ".lnk"^)
echo    objShortCut.TargetPath = Dblquote(ApplicationPath^)
echo    ObjShortCut.IconLocation = "Winver.exe,0"
echo    objShortCut.Save
echo End Sub
echo ^'**********************************************************************************************
echo ^'Fonction pour ajouter les doubles quotes dans une variable
echo Function DblQuote(Str^)
echo    DblQuote = Chr(34^) ^& Str ^& Chr(34^)
echo End Function
echo ^'**********************************************************************************************
) > Shortcutme.vbs
Start /Wait Shortcutme.vbs
Del Shortcutme.vbs
::***************************************Main Batch*******************************************
cls
echo Done and your main batch goes here !
echo i am a test 
Pause > Nul
::********************************************************************************************

回答by Ciobanu Adrian

I created a VB script and run it either from command line or from a Java process. I also tried to catch errors when creating the shortcut so I can have a better error handling.

我创建了一个 VB 脚本并从命令行或 Java 进程运行它。我还尝试在创建快捷方式时捕获错误,以便更好地处理错误。

Set oWS = WScript.CreateObject("WScript.Shell")
shortcutLocation = Wscript.Arguments(0)

'error handle shortcut creation
On Error Resume Next
Set oLink = oWS.CreateShortcut(shortcutLocation)
If Err Then WScript.Quit Err.Number

'error handle setting shortcut target
On Error Resume Next
oLink.TargetPath = Wscript.Arguments(1)
If Err Then WScript.Quit Err.Number

'error handle setting start in property
On Error Resume Next
oLink.WorkingDirectory = Wscript.Arguments(2)
If Err Then WScript.Quit Err.Number

'error handle saving shortcut
On Error Resume Next
oLink.Save
If Err Then WScript.Quit Err.Number

I run the script with the following commmand:

我使用以下命令运行脚本:

cscript /b script.vbs shortcutFuturePath targetPath startInProperty

It is possible to have it working even without setting the 'Start in' property in some cases.

在某些情况下,即使不设置 'Start in' 属性也可以让它工作。