Reputation: 376
How to create a symlink in windows including arguments using mklink, (and no powershell)?
I want to create on Desktop
a link OpenVPN
which links to "C:\Program Files\OpenVPN\bin\openvpn-gui.exe"
, with argument: --connect client.ovpn
So I tried:
cd %homepath%\Desktop && mklink "OpenVPN GUI" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe --connect client.ovpn"
FAILED -> symlink created but unable to point to .exe
.
cd %homepath%\Desktop && mklink "OpenVPN GUI" ""C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect client.ovpn"
FAILED -> Obviously syntax error.
Trying to use escape character ^
and \
: cd %homepath%\Desktop && mklink "OpenVPN GUI" "^"C:\Program Files\OpenVPN\bin\openvpn-gui.exe^" --connect client.ovpn"
FAILED.
I tried to set a variable set patharglink="C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect client.ovpn
, and use it, mklink "OpenVPN GUI" %patharglink%
FAILED.
Any idea how to solve this?
Upvotes: 0
Views: 2552
Reputation: 539
I found a way to create a shortcut (not a symbolic link, as others pointed out) with arguments, mainly through this thread.
The Idea is to call a VB script, because there is an easy implementation for shortcuts:
:: make sure the linkpath exists:
if not exist "%linkpath%" md "%linkpath%"
:: create temporary VBScript ...
echo Set objShell=WScript.CreateObject("Wscript.Shell")>%temp%\MakeShortCut.vbs
echo Set objShortcut=objShell.CreateShortcut("%linkpath%\%linkname%.lnk")>>%temp%\MakeShortCut.vbs
echo objShortcut.TargetPath="%progpath%\%progexe%.exe">>%temp%\MakeShortCut.vbs
echo objShortcut.Arguments="%arguments%">>%temp%\MakeShortCut.vbs
echo objShortcut.Description="%description%">>%temp%\MakeShortCut.vbs
echo objShortcut.WorkingDirectory="%progpath%">>%temp%\MakeShortCut.vbs
echo objShortcut.Save>>%temp%\MakeShortCut.vbs
::... run it ...
cscript //nologo %temp%\MakeShortCut.vbs
::... and delete it.
del %temp%\MakeShortCut.vbs
So you will want to set the following variables before running these lines:
%linkpath%
is the path where the shortcut is created%linkname%
is the name of the shortcut%progpath%
is the path to your executable%progname%
is the name of your executable%arguments%
%description%
%temp%
is not to be set, it is an environment variable
note: I modified that code a little from the code I am using, as I have a specific use for it in my code, and did not test if I made a typo here. If anyone uses this and it works, please feel free to remove this note
Upvotes: 2
Reputation: 38604
Here's a complete batch-file, to create the shortcut, not symbolic link, you require.
;@If Not Exist "%UserProfile%\Desktop\OpenVPN.lnk" (
; "%__AppDir__%rundll32.exe" advpack.dll,LaunchINFSection "%~0",,1)
;@GoTo :EOF
[Version]
Signature="$Windows NT$"
[DefaultInstall]
ProfileItems=AddLnk
[AddLnk]
Name="OpenVPN",8,16
CmdLine=16422,"OpenVPN\bin\openvpn-gui.exe"," --connect client.ovpn"
InfoTip="Connect OpenVPN using client config file"
WorkingDir=0
Just save the above as OVPNLink.cmd
and double click it!
Upvotes: 4