2016-01-27 62 views
0

空間我有followaing run.vbs腳本運行Rscript.exe用VBScript和路徑

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是在文件夾「福吧」,用戶得到的錯誤:「C:\用戶\ [用戶名] \桌面\富」不被識別爲內部命令

我不明白爲什麼Rscript.exe在運行之前查找絕對路徑,即使它使用相對路徑從其父目錄調用。

我聽說了雙引號的解決方案使用絕對路徑,但它似乎並沒有以.exe腳本(它雖與.BAT和.CMD)

感謝您的幫助而努力!

+0

是工作目錄正確執行腳本的時候? 'MsgBox CreateObject(「Wscript.Shell」)。CurrentDirectory' - 如果是的話,你可以簡單地構造一個引用的完整路徑。 – krlmlr

+0

@krlmlr其實我是vbs的新手,即使我用 替換Rexe'Rexe =「」「C:\ Users [用戶名] \ Desktop \ foo欄\ R-Portable \ App \ R-Portable \ bin \ Rscript.exe「」「' 我仍然得到相同的錯誤。我不知道這是否是正確的做法。 –

+0

@krlmlr我對它有了更深入的瞭解,似乎雙引號解決方案對.exe文件不起作用(它通過.bat和.cmd執行)。我編輯了這篇文章 –

回答

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