6

I would like to execute a batch file from a R script. The file is in a directory like \\network\path\to\batch\file.bat.

I know I can use the system command in R to run DOS commands but I can't simply use system("start file.bat"). So how would I best use R script to execute this batch file?

JohnN
  • 968
  • 4
  • 13
  • 35

4 Answers4

10

Try shell.exec("\\\\network\\path\\file.bat")

The shell.exec command uses the Windows-associated application to open the file. Note the double back-ticks.

Pro tip: write.csv(file='tmp.csv',tmpdat);shell.exec('tmp.csv') is useful (assuming you've associated CSV files with your preferred application for viewing CSV files) for quickly checking output.

David S
  • 116
  • 2
  • I may need to make a new question for this but, how can I force the R script to wait until the batch script has completely run before executing? Right now, the script simply starts the batch script and moves on to the next part of the code. – JohnN Aug 14 '15 at 18:47
1

try shell('\network\path\to\batch\file.bat')

Wyldsoul
  • 1,468
  • 10
  • 16
1

I found this problem when using RSelenium in Windows as well but using this batch file made sure to close all chromedriver processes. I was ending up with a ton of these processes after a lengthy scraping session.

My solution was to execute the batch file from within the R script every so often by using:

    shell.exec(file.path(getwd(), "kill_chromedriver.bat"))
Community
  • 1
  • 1
salvu
  • 519
  • 5
  • 14
1

This is what I did on windows platform.

cmd='\\prj\\whatkit\\test.bat'
system(cmd, intern = TRUE, show.output.on.console = TRUE)
SBMVNO
  • 582
  • 3
  • 13