2019年5月22日

[Windows] Create Shortcut

MKLINK

MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      建立目錄符號連結。預設是檔案符號連結。
        /H      建立永久連結而不是符號連結。
        /J      建立目錄連接。
        Link    指定新符號連結名稱。
        Target  指定新連結參照的路徑 (相對或絕對)。

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

用法: CScript scriptname.extension [選項...] [引數...]

選項:
 //B         批次模式: 不顯示 Script 錯誤和提示
 //D         啟用主動式偵錯
 //E:engine  使用該引擎來執行 Script
 //H:CScript 改變預設的 Script Host 為 CScript.exe
 //H:WScript 改變預設的 Script Host 為 WScript.exe (預設值)
 //I         互動式模式 (預設值,與 //B 恰相反)
 //Job:xxxx  執行一個 WSF 工作
 //Logo      顯示標誌 (預設值)
 //Nologo    不顯示標誌: 在執行階段不會出現標誌
 //S         為使用者儲存目前的命令行
 //T:nn      逾時值(單位為秒):  容許 Script 執行的最大時限
 //X         在偵錯工具中執行 Script
 //U         利用 Unicode 從主控台上重新引導 I/O

參考 https://superuser.com/questions/392061/how-to-make-a-shortcut-from-cmd

There is some very useful information on this site: http://ss64.com/nt/shortcut.html
Seems like there is some shortcut.exe in some resource kit which I don't have.
As many other sites mention, there is no built-in way to do it from a batch file.
But you can do it from a VB script:
Optional sections in the VBscript below are commented out:
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
So, if you really must do it, then you could make your batch file write the VB script to disk, invoke it and then remove it again. For example, like so:
@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
Running the above script results in a new shortcut on my desktop:
Here's a more complete snippet from an anonymous contributor (updated with a minor fix):
@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

沒有留言: