1

I'm trying to run an R script through the command prompt. My operating system is Windows 10. I'm having trouble running the code because there is a space in the file path of my argument. This is what I paste into the command prompt.

"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R"

I get this error:

The filename, directory name, or volume label syntax is incorrect.

However, when I run it using a file path with no spaces, it runs fine.

"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Desktop\Bundle_Runner.R"

The same behavior happens when I schedule the task through Task Scheduler: it doesn't work with the space, and it works when I remove the space from the file path.

I'm using Google Drive to sync work from multiple computers, so I'd like to be able to run my scripts using the file path with a space.

Any solutions?

Scott
  • 13
  • 3
  • Some time ago, I read/learned that having a space in the path (on windows) was problematic in specific situations (that I hit a lot), so I have always since then installed within `c:/R/R-3.4.3/` without `Program Files` in the path. I have had no problems since. Here's an interesting (though admittedly weak) note about it: https://cran.r-project.org/bin/windows/base/rw-FAQ.html#How-do-I-install-R-for-Windows_003f (sorry, not much of an answer, just commiserating). – r2evans Apr 05 '18 at 22:28
  • Odd thought: how about double-quoting it? For instance `"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "'C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R'"` (notice the extra ticks inside the quotes). – r2evans Apr 05 '18 at 22:30
  • @r2evans - Didn't work :( I've tried several different versions of quote configurations. `""C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R""` and `"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\'Google Drive'\RScriptsB\Bundle_Runner.R"` – Scott Apr 05 '18 at 22:34
  • 1
    I can't imagine it's a command prompt issue as it is quoted fine: `"C:\...\Rscript.exe" "C:\...\Bundle_Runner.R"`. Could `Rscript.exe` have problems with spaces here? Perhaps try with: `"C:\...\Rscript.exe" "'C:\...\Bundle_Runner.R'"`, or `"C:\...\Rscript.exe" ""C:\...\Bundle_Runner.R""`, or `"C:\...\Rscript.exe" ^""C:\...\Bundle_Runner.R"^"` – aschipfl Apr 05 '18 at 22:53
  • Just tested with https://stackoverflow.com/a/10227715/3358272 and it worked. It's not ideal, but it's a start. – r2evans Apr 05 '18 at 22:57
  • How about `C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe C:\Users\Scott\Google~1\RScriptsB\Bundle_Runner.R`? – r2evans Apr 05 '18 at 23:02
  • maybe start the scheduled task in C:\Users\Scott\Google Drive\RScriptsB ? – chinsoon12 Apr 06 '18 at 00:33
  • What about: `"C:\...\Rscript.exe" "\`C:\...\Bundle_Runner.R\`"`? Or do we have to escape certain characters? `"C:\...\Rscript.exe" "C:\\...\\Bundle_Runner.R"`, or `"C:\...\Rscript.exe" "'C:\\...\\Bundle_Runner.R'"`, `"C:\...\Rscript.exe" ^""C:\\...\\Bundle_Runner.R"^"`, or `"C:\...\Rscript.exe" "C:\\...\ ...\\Bundle_Runner.R"` (escaped space, `\ ` + _space_)... – aschipfl Apr 06 '18 at 09:25

3 Answers3

1
FOR %%a IN ("C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R") DO "C:\Program Files\R\R-3.4.3\bin\Rscript.exe" %%~sa

would be my approach - the problem appears to be with R, not cmd.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Try this:

C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe "C:\Users\Scott\Desktop\Bundle_Runner.R"

Or

C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe C:/Users/Scott/Google~1/RScriptsB/Bundle_Runner.R
JeanVuda
  • 1,738
  • 14
  • 29
  • I think the problem is with the `Bundle_Runner.R`'s path, not the path of R. However, it does beg the question of perhaps using this: https://stackoverflow.com/a/10227715/3358272 – r2evans Apr 05 '18 at 22:55
  • Yes, but ... put a space in the file path, not just the executable's path. – r2evans Apr 05 '18 at 22:59
  • @JeanVuda - I still run into the same error. `C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe "C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R"` – Scott Apr 05 '18 at 23:08
  • @JeanVuda - I tried the second version and it worked. Thanks for your help! – Scott Apr 06 '18 at 15:53
  • For my own education, what does the `~1` do? – Scott Apr 06 '18 at 15:54
  • Check this: https://superuser.com/questions/684426/how-are-the-digits-after-assigned-in-windows-short-file-names – JeanVuda Apr 06 '18 at 16:01
0

This is possibly related to an error reported at r-devel ("[Rd] Bug in RScript.exe for 3.5.0", https://stat.ethz.ch/pipermail/r-devel/2018-April/075869.html) that has been fixed the next day.

Perhaps the problem was already present in R 3.4.3 (you are using in your question).

Proposed workaround:

...add an extra first argument that has no space in it, e.g. Rscript --vanilla "foo bar.R"

To minimize the impact caused by --vanilla you could use

Rscript --no-save "foo bar.R"

instead (which just does not save the workspace at the end of the session).

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • That's good to know. I'll have to try updating to the newest version of R and test to see if I run into the problem again. – Scott Apr 30 '18 at 16:14
  • The patch is brand-new so you have to wait for 3.5.1 I guess – R Yoda Apr 30 '18 at 16:25