在Windows上创建桌面链接的脚本?

时间:2014-02-11 12:59:12

标签: windows perl batch-file command-line

我考虑在Windows机器上创建桌面链接的简单脚本或例程。有没有办法用perl或cmd批处理呢?

就像我做的那样

call shortcut.bat C:\putty.exe Putty

桌面上会显示新的桌面链接。

2 个答案:

答案 0 :(得分:6)

将其另存为shortcut.bat

@echo off

:: creates a temporary vbs script that drops a shortcut to the desktop
:: replace %1 with the path to your *.exe that you wish to shortcut
:: replace %userprofile%\Desktop\%2.lnk with the path and name of your shortcut
ECHO %userprofile%\Desktop\%2.lnk

:: CHECK FOR EXISTING myshortcut.vbs SCRIPT AND DELETE
cd %userprofile%\Desktop
    if exist %userprofile%\Desktop\myshortcut.vbs del %userprofile%\Desktop\myshortcut.vbs

:: CREATE myshortcut.vbs FILE CONTAINING ALL COMMANDS for the vb script to create a shortcut of your file

FOR /F "tokens=1* delims=;" %%B IN ("Set oWS = WScript.CreateObject("WScript.Shell")") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs   
FOR /F "tokens=1* delims=;" %%B IN ("sLinkFile = "%userprofile%\Desktop\%2.lnk"") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs

FOR /F "tokens=1* delims=;" %%B IN ("Set oLink = oWS.Createshortcut(sLinkFile)") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   oLink.TargetPath = "%1"") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   '   oLink.Arguments = """) do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   '   oLink.Description = """) do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   '   oLink.HotKey = "ALT+CTRL+F"") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   '   oLink.IconLocation = "%1,2"") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   '   oLink.WindowStyle = "1"") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   '   oLink.WorkingDirectory = "%1"") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs
FOR /F "tokens=1* delims=;" %%B IN ("   oLink.Save") do echo %%B>>%userprofile%\Desktop\myshortcut.vbs

:: EXECUTE myshortcut.vbs
CSCRIPT %userprofile%\Desktop\myshortcut.vbs

:: Delete vbs script
del %userprofile%\Desktop\myshortcut.vbs

exit

一样使用它
call shortcut.bat C:\PROGRA~2\TeraTerm\ttermpro.exe TeraTerm >nul

答案 1 :(得分:4)

@AlexTape的答案是完全正确的。 这是相同的答案,没有任何补充,只是为了简化它

@echo off
    call :createDesktopShortcut "%~1" "%~2"
    exit /b

:createDesktopShortcut pathToExeFile nameOfShortcut
    if not exist "%~f1" goto :eof
    setlocal & set "tempFile=%temp%\%~nx0.vbs.tmp" & set "name=%~2" & if not defined name set "name=%~n1"
    echo(With WScript.CreateObject("WScript.Shell").Createshortcut("%userprofile%\desktop\%~2.lnk"):.TargetPath="%~f1":.Save:End With>"%tempFile%"
    cscript //nologo //e:vbscript "%tempFile%">nul & del /f /q "%tempFile%" >nul 2>nul
    endlocal & goto :eof

相同用法。

call desktopShortcut.cmd "c:\windows\system32\calc.exe" "my calculator"