VBS - 如何设置PATH变量

时间:2018-01-04 12:21:04

标签: r vbscript environment-variables

如何在VBS中设置路径变量?

我对VBS完全不熟悉,需要它部署一个本地的Shiny应用程序(需要暂时将Rtools zip.exe添加到PATH)。我可以使用BAT来完成,代码如下:

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

我需要在VBS中复制它。到目前为止,我已经做到了这一点(虽然不能设置PATH):

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

这段代码来自a blog post by Lee Peng,虽然我对此并不十分理解。

我尝试在线搜索解决方案,但我无法理解这些(甚至不知道如何访问PATH变量)。请帮忙。

1 个答案:

答案 0 :(得分:1)

PATH变量包含目录名称,而不是可执行文件名称。 尝试使用此代码来临时调整PATH变量:

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