0

I have the followaing run.vbs script

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

It works pretty well in most cases except when the user puts the run.vbs script in a folder with spaces in its name: e.g. if run.vbs is in folder "foo bar", the user gets the error : "C:\Users\[user name]\Desktop\foo" not recognized as internal command...

I don't understand why Rscript.exe looks for the absolute path before running even if it's called from its parent directory using relative path.

I heard about the double quote solution using the absolute path but it doesn't seem to work with .exe scripts (it does though with .bat and .cmd)

Thanks for any help!

  • Is the working directory correct when your script is executed? `MsgBox CreateObject("Wscript.Shell").CurrentDirectory` -- If yes, you could simply construct a quoted full path yourself. – krlmlr Jan 27 '16 at 10:34
  • @krlmlr Actually I'm new to vbs, and even if I replace Rexe with `Rexe = """C:\Users[user name]\Desktop\foo bar\R-Portable\App\R-Portable\bin\Rscript.exe""" ` I still get the same error. I do'nt know if this is the right way to do it. – O. Jerrari Jan 27 '16 at 12:25
  • @krlmlr I had a deeper look into it and it seems that the double quote solution doesn't work with .exe files (it does though with .bat and .cmd). I edited the post – O. Jerrari Jan 27 '16 at 13:57

1 Answers1

0

Below code will help you

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

handle the results as they are written to and read from the StdOut object

Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
    sLine = oOutput.ReadLine
    If sLine <> "" Then s = s & sLine & vbCrLf
Wend
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48