Arani
Arani

Reputation: 823

VBS - How to set PATH variable

How do I set path variable in VBS?

I am totally new to VBS and need it to deploy a local Shiny app (which requires adding Rtools zip.exe to the PATH temporarily). I can do it using BAT, code follows:

SET ROPTS = --no-save --no-environ --no-init-file --no-restore --no-Rconsole
SET PATH = %cd%\Rtools\bin\zip.exe
R-Portable\App\R-Portable\bin\Rscript.exe %ROPTS% Shiny_Order2Ship.R 1> output.log 2>&1

I need to replicate this in VBS. So far I have done this (cannot set PATH though):

Rexe           = "R-Portable\App\R-Portable\bin\Rscript.exe"
Ropts          = "--no-save --no-environ --no-init-file --no-restore --no-Rconsole"
RScriptFile    = "Shiny_Order2Ship.R"
Outfile        = "output.log" 
strCommand     = Rexe & " " & Ropts & " " & RScriptFile & " 1> " & Outfile & " 2>&1"

intWindowStyle = 0     ' Hide the window and activate another window.'
bWaitOnReturn  = False ' continue running script after launching R   '

' the following is a Sub call, so no parentheses around arguments'
CreateObject("Wscript.Shell").Run strCommand, intWindowStyle, bWaitOnReturn

This code I got from a blog post by Lee Peng, though I do not really understand it enough.

I tried searching for solutions online, but I could not understand those (not even exactly how to access the PATH variable). Please help.

Upvotes: 1

Views: 5758

Answers (1)

JoSerra
JoSerra

Reputation: 361

PATH variable contains directories names, not executables names. Try this code to adjust temporaly your PATH variable:

Dim objShell, colVolEnvVars, fso
Dim sPath, your_path

Set objShell = WScript.CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")

your_path = fso.GetAbsolutePathName(".") & "Rtools\bin"
sPath = objShell.ExpandEnvironmentStrings("%PATH%")
If InStr(UCase(sPath), UCase(your_path)) = 0 Then
  Set colVolEnvVars = objShell.Environment("Volatile")
  colVolEnvVars("PATH") = your_path
  Set colVolEnvVars = Nothing
End If 

Set objShell = Nothing
Set fso = Nothing

Upvotes: 1

Related Questions