使用VBScript运行Rscript.exe并在路径

时间:2016-01-27 09:37:35

标签: vbscript spaces rscript

我有跟随run.vbs脚本

Rexe           = "R-Portable\App\R-Portable\bin\Rscript.exe"
Ropts          = "--no-save --no-environ --no-init-file --no-restore --no-Rconsole "
RScriptFile    = "runShinyApp.R"
Outfile        = "ShinyApp.log"
startChrome    = "GoogleChromePortable\App\Chrome-bin\chrome.exe --app=http://127.0.0.1:9999"
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
WScript.Sleep 1000
CreateObject("Wscript.Shell").Run startChrome, intWindowStyle, bWaitOnReturn

除非用户将run.vbs脚本放在名称中包含空格的文件夹中,否则它在大多数情况下都能很好地工作:如果run.vbs在文件夹“foo bar”中,则用户会收到错误:“C:\ Users \ [用户名] \ Desktop \ foo”无法识别为内部命令...

我不明白为什么Rscript.exe在运行之前会查找绝对路径,即使它是使用相对路径从其父目录调用的。

我听说使用绝对路径的双引号解决方案,但它似乎不适用于.exe脚本(它确实与.bat和.cmd一起使用)

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

以下代码可以帮助您

Dim oShell As Object
 Set oShell = CreateObject("WScript.Shell")
    'run command'
    Dim oExec As Object
    Dim oOutput As Object
    Set oExec = oShell.Exec("C:\Program Files\R\R-3.2.3\bin\Rscript.exe C:\subfolder\YourScript.R " & """" & var1 & """")
    Set oOutput = oExec.StdOut

在写入和读取StdOut对象

时处理结果
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
    sLine = oOutput.ReadLine
    If sLine <> "" Then s = s & sLine & vbCrLf
Wend