0

How to create a in including arguments using , (and no )?

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:

  1. 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.

  2. cd %homepath%\Desktop && mklink "OpenVPN GUI" ""C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect client.ovpn"
    FAILED -> Obviously syntax error.

  3. Trying to use escape character ^ and \: cd %homepath%\Desktop && mklink "OpenVPN GUI" "^"C:\Program Files\OpenVPN\bin\openvpn-gui.exe^" --connect client.ovpn"
    FAILED.

  4. 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?

Compo
  • 36,585
  • 5
  • 27
  • 39
Acca Emme
  • 356
  • 2
  • 11
  • You need to explain what type of symlink you're trying to create? A symlink cannot include switches/arguments, what you appear to be looking for is a shortcut? Please [edit your question](https://stackoverflow.com/posts/60764621/edit) to clarify. – Compo Mar 19 '20 at 21:13
  • A symlink (symbolic link) is a filesystem reparse point that redirects to another file, which is processed at a low level by the filesystem, I/O manager, and object manager in the kernel. What you want is a shell link file (i.e. a .LNK file), which the shell uses at a high level in user mode in order to open a target file, with the option of command line arguments, if the file type supports arguments (e.g. an executable or script). – Eryk Sun Mar 20 '20 at 06:21

2 Answers2

3

Here's a complete , 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!

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks, very interesting this way. Can you explain me well how it works?. But is not useful for me, because I can't add batch commands after this rows – Acca Emme Mar 22 '20 at 22:37
  • Of course you can add more commands, @AccaEmme, _(but that's outside of your initial question)_. Each of your commands go between line `2` and line `3`, and must be prefixed with a semicolon too. As for an explanation, unfortunately I have no interest in teaching for free, so at this time I'm afraid you're out of luck. Please do not take offence at my decision. – Compo Mar 23 '20 at 00:12
1

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

Daniel Bauer
  • 478
  • 3
  • 16