Search code examples
batch-filevbscriptshortcutlnk

How to get this link to start in another folder?


I have a batch script that creates a vbs that creates a shortcut (complicated I know), however the problem is that it launches the program where the shortcut is located. Any way to make it start in C:\users\Public\Settings\ where the exe is? Thanks everyone!

SETLOCAL ENABLEDELAYEDEXPANSION
SET LinkName=ITCMD-Notifier-Settings
SET Esc_LinkDest=C:\Users\%username%\Desktop\!LinkName!.lnk
SET Esc_LinkTarget=C:\users\Public\ITCMD\Settings\ITCMD Notifier Settings.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

Solution

  •  )1>!cSctVBS!
    SET "originaldir=!CD!"
    PUSHD %Esc_LinkTarget%
    cscript //nologo "!originaldir!\!cSctVBS!"
    POPD
    DEL !cSctVBS! /f /q
    

    No guarantee, naturally. Should save the original directory name, then switch to the executable's directory, then switch back before deleting the file.

    I've included quotes around the cscript target in case the current directory contains separators. I've no notion of how cscript would see that...

    Another possibility would be to switch to the executable's directory first just before the echoes which create the VBS file, then pop back after deleting that file.