0

I'll start by apologising for having to ask such a basic question, I'm sure it's a really simple fix but I can't find the information I'm looking for (or adapt the information I've found to my situation). I'm new to writing batch files, I only started because I want to make the execution of an R-Script easy for people who don't use R.

So the following code runs an R-Script just fine, the script exists in a folder called 'R Files' and the script itself directs its outputs to the 'Output' folder, where the 'R Files' and 'Output folders share the same parent folder.

R --vanilla --quiet <growth_plot_driver.R >console_output.txt

However, considering that other users of this code are unlikely to place the parent folder in the same location that I did I thought it would be best to specify the input and output files in variables. This is where I run into problems, of the "The system cannot find the file specified" kind. I've tried many variations of the following code.

set "path1=%cd%\growth_plot_driver.R"
cd..
cd Output
set "path2=%cd%\console_output.txt"
R --vanilla --quiet <%path1% >%path2%

But as I said, I'm new to this and if there's a better way to go about this I would grateful to hear about it.

frank2165
  • 114
  • 2
  • 7
  • 1
    Is there a space in your directory? If so, this won't work. You need to put the quotes around just the path, not the variable too. For example, try using `set path1="%cd%\growth_plot_driver.R"` and `set path2="%cd%\console_output.txt"`. Also, consider using `Rscript` for these purposes, you won't get the whole console loading up. – nograpes Oct 20 '14 at 03:17

1 Answers1

3

nograpes has suggested already one possible solution:

set path1="%cd%\growth_plot_driver.R"
cd ..\Output
set path2="%cd%\console_output.txt"
R --vanilla --quiet <%path1% >%path2%

This defines the environment variables path1 and path2 with string values containing the double quotes.

But I prefer for such cases:

set "path1=%cd%\growth_plot_driver.R"
cd ..\Output
set "path2=%cd%\console_output.txt"
R --vanilla --quiet <"%path1%" >"%path2%"

This defines the environment variables path1 and path2 with string values NOT containing the double quotes. But the required double quotes in case of a space in path are now used on referencing the values of the two environment variables. In other words the environment variables contain really just the file names with full path and the required double quotes are used on command where they are really needed.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you @nograpes and Mofi, my problem does seem to have been the result of a space in the directory name. (going from R programming to writing batch files for windows command line is doing my head in) – frank2165 Oct 20 '14 at 06:18