【发布时间】: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)。我编辑了帖子