使用批处理文件为exe创建快捷方式

3

我知道已经有一个类似的主题,但我不想使用VB脚本。

我希望您能在DOS中使用命令行创建快捷方式。

请提供一些示例,那将非常好。

谢谢!

AA


一个类似的主题,但回答稍微详细一些:https://superuser.com/questions/392061/how-to-make-a-shortcut-from-cmd - hemn
5个回答

3

如果不调用外部程序,您无法在.bat文件中创建快捷方式。

然而,自Windows 2000以来的每个版本都具有内置脚本语言,称为 Windows Script Host

这是我几年前编写的一个小型WSH脚本,可以从.bat文件中调用,只需将此文本保存为 shortcut.wsf,其中包含脚本中的使用信息。

<package>
 <job id="MakeShortcut">
  <runtime>
   <description>Create a shortcut (.lnk) file.</description>
   <named
     name = "Target"
     helpstring = "the target script"
     type = "string"
     required = "true"
   />
   <named
     name = "Args"
     helpstring = "arguments to pass to the script"
     type = "string"
     required = "false"
   />
   <unnamed
     name = "basename"
     helpstring = "basename of the lnk file to create"
     type = "string"
     required = "false"
   />
  </runtime>

  <script language="JScript">

   if ( ! WScript.Arguments.Named.Exists("Target"))
   {
      WScript.Arguments.ShowUsage();
      WScript.Quit(2);
   }

   target = WScript.Arguments.Named.Item("Target");
   WScript.Echo("target " + target);
   args   = WScript.Arguments.Named.Item("Args");
   WScript.Echo("args " + args);
   base = WScript.Arguments.Unnamed.Item(0);
   WScript.Echo("base " + base);

   fso   = WScript.CreateObject("Scripting.FileSystemObject");
   //path  = fso.GetParentFolderName(WScript.ScriptFullName);
   path  = fso.GetAbsolutePathName(".");
   WScript.Echo("path = " + path);
   Shell = WScript.CreateObject("WScript.Shell");

   short = fso.BuildPath(path,base);
   if ( ! fso.GetExtensionName(base))
      short = short + ".lnk";

   link  = Shell.CreateShortcut(short);
   link.TargetPath   = fso.BuildPath(path, target);
   if (args != null && args != "")
      link.Arguments = args;
   else
      link.Arguments = base;
   //link.Description = "Sound Forge script link";
   //link.HotKey = "ALT+CTRL+F";
   //link.IconLocation = fso.BuildPath(path, target) + ", 2";
   //link.WindowStyle = "1"
   //link.WorkingDirectory = path;
   link.Save();

  </script>
 </job>
</package>

不带任何参数运行它以获取用法

c:\> shortcut.wsf
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Create a shortcut (.lnk) file.
Usage: shortcut.wsf /Target:value [/Args:value] [basename]

Options:

Target   : the target script
Args     : arguments to pass to the script
basename : basename of the lnk file to create

还有 PowerShell,它更好用 ;) - Hamish Grubijan
2
自Win98以来,所有版本的Windows都有WSH,PowerShell仅在Win7和Server 2008中默认包含。就代码可移植性而言,没有任何争议。 - John Knoeller

2

2
subst 是一个仅针对驱动器字母的 dos hack,与快捷方式无关。 - John Knoeller
@John:老兄,还记得那些subst命令吗?它用于为真实的嵌套子目录创建驱动器字母...就像别名一样。 - t0mm13b
符号链接对于绝大多数程序来说并不是一个真正的选项。Windows不是UNIX,许多Windows程序假定文件存储在它们的应用程序目录中。如果你使用符号链接,它们几乎肯定不会再起作用了。我猜你想要一个Shell链接(又称快捷方式),John的解决方案实际上是正确的。 - Joey

2

现在可以使用PowerShell完成,这比VBscript要好一些。而且PowerShell可以从.bat / .cmd文件中调用:

powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\mylink.lnk'); $s.TargetPath='C:\Path\to\your.exe'; $s.Save()"

这里还有另一个例子:https://ss64.com/nt/shortcut.html#e

另请参阅


1

从批处理文件中创建.lnk格式的快捷方式基本上是不可能的,除非调用某种外部程序。可以在这里找到文件规范,并且快速浏览就可以解释清楚。

创建.url格式的快捷方式相当容易,因为该格式是一个简单的文本文件。规范可以在这里找到。该格式有一些缺点,但可能实现您的目标。


1
你链接的不是规范,而是该格式的反向工程。你可以在http://msdn.microsoft.com/en-us/library/dd871305(PROT.10).aspx找到实际的规范。 - Joey

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接