如何从CMD创建快捷方式?

Sha*_*anu 95 windows shortcuts command-line windows-xp

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

Der*_*ler 63

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

似乎shortcut.exe某些资源工具包中有一些我没有。
正如许多其他站点所提到的,没有内置的方法可以从批处理文件中执行此操作。

但是您可以通过 VB 脚本来实现:

下面 VBscript 中的可选部分被注释掉了:

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
Run Code Online (Sandbox Code Playgroud)

所以,如果你真的必须这样做,那么你可以让你的批处理文件将 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
Run Code Online (Sandbox Code Playgroud)

运行上面的脚本会在我的桌面上产生一个新的快捷方式:
结果快捷方式

这是来自匿名贡献者的更完整的片段(更新了一个小修复):

@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
Run Code Online (Sandbox Code Playgroud)

  • 与其为每次执行创建一个 vbscript,不如使用“Wscript.Arguments”来获取命令行参数......哈哈 (2认同)

小智 33

这是使用 powershell 的类似解决方案(我知道,您可能可以在 PS 中重写整个批处理文件,但如果您只想完成它™...)

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()"
Run Code Online (Sandbox Code Playgroud)

您可能必须在文件中明确指定 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} 
Run Code Online (Sandbox Code Playgroud)


Whi*_*oop 18

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

  • 我推荐几乎*所有*来自 NirSoft,它是终极极客工具集 (13认同)

小智 15

使用 mklink 命令怎么样?C:\Windows\System32>mklink 创建符号链接。

MKLINK [[/D] | [/H] | [/J]] 链接目标

    /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.
Run Code Online (Sandbox Code Playgroud)

  • 好主意,但符号链接的行为似乎与快捷方式略有不同。如果我创建 Visual Studio 解决方案的快捷方式,它会正确打开所有相对路径的项目。但是,如果我通过符号链接打开相同的解决方案,工作目录是符号链接所在的路径,而不是它所引用的路径。 (11认同)
  • 另外创建 simlink 需要管理员权限。 (2认同)

Jim*_*ine 13

如果您安装了Git,它会捆绑在一起create-shortcut.exe,允许您从命令行创建快捷方式,并且可以在 Windows 10 中使用。该实用程序没有 AFAICT 公开记录,并且是--help最小的:

Usage: create-shortcut.exe [options] <source> <destination>
Run Code Online (Sandbox Code Playgroud)

但是,使用 Sysinternals 的strings实用程序从 中提取字符串.exe,我能够计算出[options]和到快捷方式页面中显示的字段的映射Properties

--work-dir ('Start in' field)
--arguments (tacked onto the end of the 'Target')
--show-cmd (I presume this is the 'Run' droplist, values 'Normal window', 'Minimised', 'Maximised')
--icon-file (allows specifying the path to an icon file for the shortcut)
--description ('Comment' field)
Run Code Online (Sandbox Code Playgroud)

用法示例:

REM If bin folder already in your PATH, omit CD line:
cd /d "C:\Program Files\Git\mingw64\bin" 
create-shortcut.exe --work-dir "C:\path\to\files" --arguments "--myarg=myval" "C:\path\to\files\file.ext" "C:\path\to\shortcuts\shortcut.lnk"
Run Code Online (Sandbox Code Playgroud)

strings实用程序还揭示了应用程序与 Windows Vista、7、8、8.1 和 10 的兼容性。

为了方便起见,bin可以将该文件夹添加到您的文件夹中PATH,如下所示:

"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" "[Environment]::SetEnvironmentVariable(\"PATH\", \"$env:PATH;C:\Program Files\Git\mingw64\bin\", [System.EnvironmentVariableTarget]::User)"
Run Code Online (Sandbox Code Playgroud)

C:\Program Files\Git\mingw64\bin如果create-shortcut.exe存在于系统上的不同路径中,请调整)


Nir*_*asi 8

在我们在这里进行了所有讨论之后,这是我建议的解决方案:下载: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)

  • 由于他在问题正文中放置了“快捷方式(.lnk 文件)”,我认为他想创建一个实际的快捷方式。 (2认同)

归档时间:

查看次数:

349912 次

最近记录:

4 年,4 月 前