1

How can I assign with in Rscript to pause until the command prompt is open? I am running a Rscript where a batch file will be executed through command prompt. Later I want to copy a product of executed batch file to another folder. However, Rscript tries to copy while it is being written.

This batch file will open multiple cmd prompts to execute multiple files, so I don't want to use "shell", "system", or "system2" instead of "shell.exec2".

lapply(1:num_cores, function(o) {
  ## batch file location
  loc <- paste0(projectfolder_location, pp, "\\ppfolder_", o, ".Sufi2.SwatCup")
  ## executing batch file
  shell.exec2(paste0(pp_folder, "\\SUFI2_Run.bat")) 
})

#PAUSING HERE until cmd prompt is open!!
file.copy(list.files(
  path=paste0(projectfolder_location, pp, "\\ppfolder_", 1, ".Sufi2.SwatCup"), 
  pattern="Name.out$", full.name=TRUE), projectfolder, overwrite=TRUE)

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Arun Bawa
  • 13
  • 3
  • Why not use [**`shell`**](https://www.rdocumentation.org/packages/base/versions/3.4.0/topics/shell) which defaults to `wait=TRUE`? Maybe [**`shell.exec2`**](https://www.rdocumentation.org/packages/R.utils/versions/2.10.1/topics/shell.exec2) is a wrapper but accepts no other args. – Parfait Mar 26 '22 at 14:55
  • Shell executes within R, which gives an invalid handle error. Also, it opens multiple cmd prompt to execute parallel processes. – Arun Bawa Mar 26 '22 at 16:19
  • Did you try `shell("cmd.exe /c /path/to my.bat")`? This launches a shell outside R. Also, why use R to run these processes? Why not CMD or PowerShell to run each in successive order and copy files thereafter? If to keep with your workflow, R can call a PowerShell, etc. script. – Parfait Mar 26 '22 at 16:49

0 Answers0