Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script that create Desktop Links on Windows? [duplicate]

i thinking about a simple script or routine to create desktop links on windows machines. is there a way to do it with perl or cmd batch?

like if i do

call shortcut.bat C:\putty.exe Putty

a new desktop link appears on desktop.

like image 705
user3294953 Avatar asked Dec 26 '22 13:12

user3294953


2 Answers

save this as 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

use it like

call shortcut.bat C:\PROGRA~2\TeraTerm\ttermpro.exe TeraTerm >nul
like image 60
Alex Tape Avatar answered Jan 11 '23 15:01

Alex Tape


@AlexTape answer is completely correct. This is the same answer, with nothing to add, just to simplify it

@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

Same usage.

call desktopShortcut.cmd "c:\windows\system32\calc.exe" "my calculator"
like image 21
MC ND Avatar answered Jan 11 '23 17:01

MC ND