1

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
2
  • 3
    place echo oLink.WorkingDirectory = "C:\users\Public\Settings" before echo oLink.Save Also is best practice enclose both var name and var content within quotes as in SET "Esc_LinkTarget=C:\users\Public\ITCMD\Settings\ITCMD Notifier Settings.exe" Commented Dec 7, 2017 at 17:50
  • shouldn't it be `C:\users\Public\Settings`? Commented Dec 7, 2017 at 18:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.