2012-02-20 18:24:10 +0000 2012-02-20 18:24:10 +0000
81
81
Advertisement

如何从CMD制作快捷方式?

Advertisement

如何使用命令行实用程序创建一个快捷文件(.lnk)到另一个文件或可执行文件?

Advertisement

答案 (8)

56
56
56
2012-02-20 20:06:04 +0000

这个网站上有一些非常有用的信息。http://ss64.com/nt/shortcut.html

似乎在一些资源包里有一些shortcut.exe,而我没有。 正如许多其他网站提到的,没有内置的方法可以从批处理文件中完成。

但你可以通过VB脚本来做:

下面的VB脚本中的可选部分被注释掉了:

Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\MyShortcut.LNK"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "C:\Program Files\MyApp\MyProgram.EXE"
' oLink.Arguments = ""
' oLink.Description = "MyProgram"   
' oLink.HotKey = "ALT+CTRL+F"
' oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
' oLink.WindowStyle = "1"   
' oLink.WorkingDirectory = "C:\Program Files\MyApp"
oLink.Save

所以,如果你真的****必须要做,那么你可以让你的批处理文件把VB脚本写到磁盘上,调用它,然后再把它删除。例如,像这样:

@echo off
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%HOMEDRIVE%%HOMEPATH%\Desktop\Hello.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\Windows\notepad.exe" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs

运行上述脚本的结果是在我的桌面上有一个新的快捷方式:

以下是一个匿名作者提供的更完整的片段(经过小的修正后更新):

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET LinkName=Hello
SET Esc_LinkDest=%%HOMEDRIVE%%%%HOMEPATH%%\Desktop\!LinkName!.lnk
SET Esc_LinkTarget=%%SYSTEMROOT%%\notepad.exe
SET cSctVBS=CreateShortcut.vbs
SET LOG=".\%~N0_runtime.log"
((
  echo Set oWS = WScript.CreateObject^("WScript.Shell"^) 
  echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)
  echo Set oLink = oWS.CreateShortcut^(sLinkFile^) 
  echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)
  echo oLink.Save
)1>!cSctVBS!
cscript //nologo .\!cSctVBS!
DEL !cSctVBS! /f /q
)1>>!LOG! 2>>&1
24
24
24
2014-11-06 18:05:38 +0000

这里有一个类似的解决方案,使用 powershell(我知道,你可能可以在PS中重写你的整个批处理文件,但如果你只想Get It Done™…)

set TARGET='D:\Temp'
set SHORTCUT='C:\Temp\test.lnk'
set PWS=powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile

%PWS% -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut(%SHORTCUT%); $S.TargetPath = %TARGET%; $S.Save()"

你可能需要在文件中明确指定PS的路径,但应该可以。还有一些额外的属性,你也可以通过这个对象来修改:

Name MemberType Definition                             
---- ---------- ----------                             
Load Method void Load (string)                     
Save Method void Save ()                           
Arguments Property string Arguments () {get} {set}        
Description Property string Description () {get} {set}      
FullName Property string FullName () {get}               
Hotkey Property string Hotkey () {get} {set}           
IconLocation Property string IconLocation () {get} {set}     
RelativePath Property string RelativePath () {set}           
TargetPath Property string TargetPath () {get} {set}       
WindowStyle Property int WindowStyle () {get} {set}         
WorkingDirectory Property string WorkingDirectory () {get} {set}
17
Advertisement
17
17
2013-09-30 23:24:55 +0000

除了 shortcut.exe,你还可以使用命令行版本的 NirCmd 创建快捷方式 http://nircmd.nirsoft.net/shortcut.html

12
12
12
2014-04-24 16:26:05 +0000

使用mklink命令如何?C:Windows系统32>mklink创建一个符号链接。链接目标

/D Creates a directory symbolic link. Default is a file
            symbolic link.
    /H Creates a hard link instead of a symbolic link.
    /J Creates a Directory Junction.
    Link specifies the new symbolic link name.
    Target specifies the path (relative or absolute) that the new link
            refers to.
7
Advertisement
7
7
2012-02-20 18:42:56 +0000

经过我们在这里的讨论,我建议的解决方案是:下载。http://optimumx.com/download/Shortcut.zip 在桌面上解压(比如说)。现在,假设你想为一个叫scrum.pdf的文件创建一个快捷方式(也是在桌面上): 1.打开CMD,进入桌面文件夹 2.运行:Shortcut.exe /f:"%USERPROFILE%\Desktop\sc.lnk" /a:c /t:%USERPROFILE%\Desktop\scrum.pdf

,它将在你的桌面上创建一个名为sc.lnk的快捷方式,指向原始文件(scrum.pdf)。

0
0
0
2015-07-21 15:01:08 +0000

我知道这个话题已经很久了,但我想提供一个简单的解决方案。然后,我在桌面上创建了快捷方式,并将图标设置为C:驱动器上的ico文件。然后,我把.ico和快捷方式都复制到我的用户可以访问的网络共享中。在那里,我写了下面的批处理文件,将ico和.url复制到用户的windows 7桌面上。这样就在所有用户的桌面上创建了快捷方式,并保留了我在创建快捷方式时设置的图标文件。希望对大家有帮助。

0
Advertisement
0
0
2015-04-12 19:14:32 +0000

这个免费程序有必要的功能http://www.nirsoft.net/utils/nircmd2.html。(样本来自上述网页)"Create a shortcut to Windows calculator under Start Menu->Programs->Calculators nircmd.exe shortcut "f:\winnt\system32\calc.exe" "~$folder.programs$\Calculators" "Windows Calculator"

我自己的样本尝试:nircmd.exe快捷方式 “c:\windows\system32\calc.exe”“~$folder.desktop$”“Windows计算器”

0
0
0
2020-01-24 16:58:48 +0000
Advertisement
Advertisement