简体   繁体   中英

Running a .bat file from R - had status 2

I am trying to run a .bat file, which does run perfectly when I double click in it (Windows OS), but fails when I try to run it in R

 com <- "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"

 system(com)

I am getting back a message of had status 2

Just an FYI, this triggers a SAS program, which I need to run in SAS as it is for comparison purposes between SAS and R.

In Windows, to run batch files from command line you need to call a command line interpreter, Command Prompt or PowerShell, passing batch file as an argument.

A .bat script by itself is like an .R script and does not do anything until an executable runs it (ie, Rscript.exe, R.exe, Rcmd.exe, Rterm.exe ) and in this case, cmd.exe and powershell.exe :

# COMMAND PROMPT
system('cmd /c "C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat"')

# POWERSHELL
system('powershell -c & "\'C:\\SASLocal\\RUN3614\\56a8c11b-84b2-4af7-a155-01190936b1c1\\M1_superGOtest.bat\'"')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM