如何在 Windows 中通过命令行创建快捷方式? [英] How do I create a shortcut via command-line in Windows?

查看:68
本文介绍了如何在 Windows 中通过命令行创建快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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:UsersGabrielDesktop	est.bat" "C:UsersGabrielAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup"

你能帮我吗?

推荐答案

您可以使用 PowerShell 命令.将其粘贴到您的批处理脚本中,它会在 %userprofile%Start MenuProgramsStartup 中创建一个到 %~f0 的快捷方式:

You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0 in %userprofile%Start MenuProgramsStartup:

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

<小时>

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


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

mklink saveShortcutAs targetOfShortcut

请参阅控制台窗口中的 mklink/? 以了解完整语法,以及 此网站页面了解更多信息.

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

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

In your batch script, do:

mklink "%userprofile%Start MenuProgramsStartup\%~nx0" "%~f0"

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

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.

这篇关于如何在 Windows 中通过命令行创建快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆