title: When trying to run R script from CMD, getting "Fatal error: cannot open file 'CMD': No such file or directory"
body: <p>I'm trying to create a batch file to run an R script for me automatically on Windows 7.</p>
  
  <p><strong>Batch file:</strong></p>
  
  <pre><code>"C:\Program Files\R\R-3.2.3\bin\Rscript.exe" CMD BATCH "C:\Users\&lt;my username&gt;\Documents\R\Script Testing\script.R"
  pause
  </code></pre>
  
  <p><strong>R script:</strong></p>
  
  <pre><code>write.csv("hello", "automatic_output.txt")
  </code></pre>
  
  <p>This script works when I run it in R, but when I try to run the .bat, I get this error:</p>
  
  <blockquote>
    <p>Fatal error: cannot open file 'CMD': No such file or directory</p>
  </blockquote>
  
  <p>This happens regardless of whether I paste the code into the command prompt, run the .bat as administrator, or schedule the task.</p>
  
  <p>I tried following the advice given in this thread:
  <a href="https://stackoverflow.com/questions/17022957/exporting-csv-from-r-batch-file/17023181#17023181">Exporting .csv from R &amp; Batch file</a>
  but am seeing the same behavior.</p>
  
  <p>I also used the Windows search to try to find "automatic_output.txt" in case it was being stored in an unexpected location but no results came up.</p>
  
  <p>Lastly,
  <a href="https://stackoverflow.com/a/23514987">https://stackoverflow.com/a/23514987</a> seemed to suggest that I could omit the "CMD BATCH", and when I do I no longer get an error, but automatic_output.txt is still not produced.</p>
  
  <p>Any ideas?</p>