6

I am trying to run the following R script in windows shell:

Rscript C:/Documents/Folder name containing space/myscript.txt

In this case I get the error:

Fatal error: cannot open file 'C:/Documents/Folder': No such file or directory

However when I use quotation marks (tried single double and triple as was suggested in other posts) I get the following error:

Rscript "C:/Documents/Folder name containing space/myscript.txt"
The filename, directory name, or volume label syntax is incorrect.

I can't find a way to get around the space problem and changing the file location so there are no white-spaces is not an option for me.

Any help would be greatly appreciated.

Further Clarifications:

The issue I am having is not directly related to R but rather to to having the file path that contains spaced being passed to Rscript.

In the documentations, Rsript should be used in the following way:

Rscript [options] [-e expr [-e expr2 ...] | file] [args]

It is also noted that:

Spaces are allowed in expression and file (but will need to be protected from the shell in use, if any, for example by enclosing the argument in quotes).

However trying to enclose the file path in quotes results in the error

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

To avoid confusion, running Rscript C:/Documents/Folder_name/myscript.txt works fine when the path doesn't contain any spaces as does Rscript "C:/Documents/Folder_name/myscript.txt".

  • Would that make it `Rscript C:/Documents/Folder\\ name\\ containing\\ space/myscript.txt`? – lit Jan 31 '18 at 15:56
  • Open a __command prompt window__ and run `cmd /?`. The last paragraph on last help page output into __console window__ (not shell window) explains that a file name argument must be enclosed in double quotes on containing a space or one of these characters ``&()[]{}^=;!'+,`~``. And directory separator on Windows is the backslash character ``\``. Don't know if `Rscript` on Windows expects file names nevertheless with `/` as on Unix/Linux/Mac. `Rscript.exe "C:\Documents\Folder name containing space\myscript.txt"` __might__ work. (I could not test it because Rscript not installed.) – Mofi Jan 31 '18 at 21:38
  • Thank you for having a look but it doesn't solve the problem. The issue I am having is not with R itself but rather in passing on the file path by the shell. using `"file path"` doesn't work, returning `The filename, directory name, or volume label syntax is incorrect.`. I am looking for a way for the shell to pass the file path that includes spaces. In the documentations it is specified the **Spaces are allowed in expression and file (but will need to be protected from the shell in use, if any, for example by enclosing the argument in quotes).** which doesn't seem to work. – Gabriel Olshansky Feb 01 '18 at 04:54
  • @rashid and @lit using the \\ doesn't solve the problem, instead returning the `C:/Documents/Folder\\' is not recognized as an internal or external command, operable program or batch file` – Gabriel Olshansky Feb 01 '18 at 05:32
  • Have you tried wrapping the file path in quotes? So `Rscript "C:/Documents/Folder name containing space/myscript.txt"` – Steven Feb 01 '18 at 06:09
  • @Steven Yes I have tried that and I get the message `The filename, directory name, or volume label syntax is incorrect.` – Gabriel Olshansky Feb 01 '18 at 06:38
  • @Mofi It is an executable (.exe) file – Gabriel Olshansky Feb 01 '18 at 06:40
  • @GabrielOlshansky In my opinion `rscript.exe` has a bug if not supporting on Windows `"C:\Documents\Folder name containing space\myscript.txt"` with Windows directory separator nor `"C:/Documents/Folder name containing space/myscript.txt"` with Unix/Linux/Mac directory separator. You can additionally try `"C:\\Documents\\Folder name containing space\\myscript.txt"`. But if that also does not work, you better contact author of __Rscript__ and report this bug and use in the meantime the short 8.3 path/file name of that file to work around this bug. – Mofi Feb 01 '18 at 06:45
  • @GabrielOlshansky One last try would be `"'C:/Documents/Folder name containing space/myscript.txt'"`. Windows command line interpreter `cmd.exe` does not support `'` to quote an argument with spaces as Linux shell interpreters like `bash`, `sh` or `ksh`. So double quotes must be used on Windows for an argument string with spaces. But perhaps __Rscript__ is so bad coded that it interprets only file names with spaces enclosed in `'` correct. And as __Rscript__ is ported from Unix, it might help to use as first argument `--` and as second argument the file name enclosed in `"`. – Mofi Feb 01 '18 at 06:50
  • @Mofi I have tried all of that and I still get the same message. I think you are right, I am going to report the bug. Thank you for all the help, I will update the post when I hear back from them – Gabriel Olshansky Feb 01 '18 at 11:38
  • @GabrielOlshansky did you get any results from reporting the bug? I'm having exactly the same issue after upgrading to R 3.5.0. I never had this issue before! i can't run Rscript with spaces in the names of folders and files. – Ankhnesmerira May 14 '18 at 03:46

4 Answers4

3

It is a BUG in R version 3.5.0 for Windows.
One workaround, apart from downgrading, is creating an R script with no spaces in its path and run the spaced one with source():

## C:\Documents\Folder-name-no-space\myscript.txt
source("C:/Documents/Folder name containing space/myscript.txt")

Then you run it with:

Rscript C:\Documents\Folder-name-no-space\myscript.txt

or also:

Rscript C:/Documents/Folder-name-no-space/myscript.txt

You may also try the 8.3 filename. You can get it with:

for %I in ("C:/Documents/Folder name containing space/myscript.txt") do @echo %~sI

UPDATE

Since 3.5.1 the problem has been fixed.

antonio
  • 10,629
  • 13
  • 68
  • 136
1

Forward slashes work just fine with R, so don't worry about backward slashes. I've just verified and the following works at the CMD.exe terminal of Windows 8.1:

C:\Windows\System32> Rscript "C:/Users/hb/folder with spaces/script.R"
[1] "1+2+3"

C:\Windows\System32>

My best guess is that you've got the pathname incorrect. If it's a non-existing pathname, you do get:

C:\Windows\System32> Rscript --vanilla "C:/Users/hb/folder with spaces/non-existing.R"
Fatal error: cannot open file 'C:/Users/hb/folder with spaces/non-existing.R': No such file or directory

You can validate it from within R, e.g.

> file.exists("C:/Users/hb/folder with spaces/script.R")
[1] TRUE
HenrikB
  • 6,132
  • 31
  • 34
1

Simple solution: install a newer version of R.

From the version 3.5.1 release notes, the relevant bug fix is described here:

Allow file argument of Rscript to include space even when it is first on the command line.

Mike T
  • 41,085
  • 18
  • 152
  • 203
0

Another convenient workaround was to create an alias for the portion of the path which contains spaces, i.e.:

SUBST k: "c:\Folder with Spaces"
rscript k:\scripts\program.R