我希望我的 .bat 脚本 (test.bat) 为它自己创建一个快捷方式,以便我可以将它复制到我的 Windows 8 启动文件夹中。
我已经编写了这行代码来复制文件,但我还没有找到创建上述快捷方式的方法,因为您可以看到它只复制脚本。
xcopy "C:\Users\Gabriel\Desktop\test.bat" "C:\Users\Gabriel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
你能帮帮我吗?
最佳答案
您可以使用 PowerShell 命令。将其粘贴到您的批处理脚本中,它将在 %userprofile%\Start Menu\Programs\Startup
中创建一个指向 %~f0
的快捷方式:
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Start Menu\Programs\Startup\%~n0.lnk');$s.TargetPath='%~f0';$s.Save()"
如果您不想使用 PowerShell,您可以使用 mklink
来创建符号链接(symbolic link)。语法:
mklink saveShortcutAs targetOfShortcut
请参阅控制台窗口中的 mklink/?
以获取完整语法,以及 this web page了解更多信息。
在您的批处理脚本中,执行:
mklink "%userprofile%\Start Menu\Programs\Startup\%~nx0" "%~f0"
创建的快捷方式不是传统的 .lnk 文件,但它应该可以正常工作。请注意,这仅在 .bat 文件从与启动文件夹相同的驱动器运行时才有效。此外,显然需要管理员权限才能创建符号链接(symbolic link)。
关于windows - 如何在 Windows 中通过命令行创建快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30028709/