【问题标题】:Run Rscript.exe with VBScript and spaces in path使用 VBScript 和路径中的空格运行 Rscript.exe
【发布时间】:2016-01-27 09:37:34
【问题描述】:

我有以下 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)

感谢您的帮助!

【问题讨论】:

  • 脚本执行时工作目录是否正确? MsgBox CreateObject("Wscript.Shell").CurrentDirectory -- 如果是,您可以简单地自己构造一个带引号的完整路径。
  • @krlmlr 实际上我是 vbs 的新手,即使我用 Rexe = """C:\Users[user name]\Desktop\foo bar\R-Portable\App\R-Portable\bin\Rscript.exe""" 替换 Rexe,我仍然会遇到同样的错误。我不知道这是否是正确的做法。
  • @krlmlr 我对它进行了更深入的研究,似乎双引号解决方案不适用于 .exe 文件(它适用于 .bat 和 .cmd)。我编辑了帖子

标签: vbscript spaces rscript


【解决方案1】:

下面的代码会帮助你

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

【讨论】:

    猜你喜欢
    • 2013-09-02
    • 1970-01-01
    • 2012-12-10
    • 2023-03-09
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2016-01-17
    • 2019-04-26
    相关资源
    最近更新 更多