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

时间:2022-04-04 07:08:08

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 Startup文件夹。

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?

你能帮我吗?

7 个解决方案

#1


You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0 in %userprofile%\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()"

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

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

mklink saveShortcutAs targetOfShortcut

See mklink /? in a console window for full syntax, and this web page for 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文件与启动文件夹在同一驱动器上运行时,才能使用此选项。此外,显然需要管理员权限才能创建符号链接。

#2


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

无法使用纯批处理。检查shortcutJS.bat - 它是一个jscript / bat混合,应该与.bat扩展名一起使用:

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

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

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

#3


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中对我最有效.TowerShell解决方案已经运行,但没有出现快捷方式。 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

#4


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中运行良好。

#5


link.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

#6


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
::********************************************************************************************

#7


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

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确实意味着你正在使用另一个第三方.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可能更简单。

#1


You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0 in %userprofile%\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()"

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

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

mklink saveShortcutAs targetOfShortcut

See mklink /? in a console window for full syntax, and this web page for 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文件与启动文件夹在同一驱动器上运行时,才能使用此选项。此外,显然需要管理员权限才能创建符号链接。

#2


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

无法使用纯批处理。检查shortcutJS.bat - 它是一个jscript / bat混合,应该与.bat扩展名一起使用:

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

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

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

#3


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中对我最有效.TowerShell解决方案已经运行,但没有出现快捷方式。 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

#4


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中运行良好。

#5


link.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

#6


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
::********************************************************************************************

#7


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

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确实意味着你正在使用另一个第三方.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可能更简单。