运行 Rscript.exe 路径中包含 VBScript 和空格

Run Rscript.exe with VBScript and spaces in path

我有以下 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\[user name]\Desktop\foo" 无法识别为内部命令...

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

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

感谢您的帮助!

下面的代码可以帮到你

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