-2

I have following vbscript:

set objShell = Createobject("wscript.shell")
objShell.Run """Scripts\Rec.bat""", 0
objShell.Run """Scripts\Prod.bat""", 0
Set objShell = Nothing

The above vbscript is stored in USB drive and i am trying to launch it from usb. Now problem is when i normally run this vbscript it executes. But when i do run as administrator from context menu following error occurs "System cannot find the file specified"

I tried to debug the problem using another below script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
Wscript.Echo strDrive

When i run it normally it echo F: that is my usb drive letter. But when i run as admin it echo C:

So the batch file location is being searced to C:\Scripts\Rec.bat instead of F:\Scripts\Rec.bat thats why this error is comming.

Can anyone help??

I want to run vbscript as admin and still retain the usb drive letter path because the code in my Rec.bat requires admin privilege

Tathastu Pandya
  • 326
  • 1
  • 8

1 Answers1

-1

You don't want it to be relative to the current directory, you want it to be relative to the directory of the running vbscript:

Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 
strPath = "" & strFolder & "\Scripts\Rec.bat" & ""

objShell.Run strPath, 0

If you want to try it with a test script, you'd be advised to replace 0 on the last line with 1.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks seems to be working but what if i have to run muliple more than one batch scripts. i am no expert at vbscript. can you help with that using some loop or alike pleaseee – Tathastu Pandya Jul 08 '22 at 11:34
  • 2
    Exactly as the first duplicate explains - "You can use WScript.ScriptFullName which will return the full path of the executing script." – user692942 Jul 08 '22 at 12:12