I have a .bat file that comes with an instrument I use. The file opens a cmd.exe terminal in windows, where I then have to change the directory and run a command to process files in that directory. I want to create an R script that automates this (and gets attached to another block of code I have written for the front side in order to automate the whole process.)
I've been trying variations of shell, shell.exec, system, and system2...but nothing is working the way I expect.
This works to run the bat file and opens the cmd.exe window:
shell.exec("C:\\Program Files\\Thermo Fisher Scientific\\Affymetrix Power Tools\\APT-1.20.5\\bin\\apt-vars.bat")
But i need to be able to change the directory to the current working directory. (The bat file opens up with the wd set to the desktop.)
and then I need to run the following line in the prompt after the directory is changed:
apt-cel-convert.exe --format text --in-place --cel-files CEL_FILE.txt"
attempt:
outdir <- getwd()
#formats the wd path
out_dir <- gsub("/", "\\\\", outdir)
#define pieces
script <- "C:\\Program Files\\Thermo Fisher Scientific\\Affymetrix Power Tools\\APT-1.20.5\\bin\\apt-vars.bat"
command <- "apt-cel-convert.exe --format text --in-place --cel-files CEL_FILE.txt"
#put it all together
path <- paste(script, "&& cd",out_dir, "&&", command, sep = " ")
#run it
shell.exec(path)
Which results in:
Error in shell.exec(path) :
'C:\Program Files\Thermo Fisher Scientific\Affymetrix Power Tools\APT-1.20.5\bin\apt-vars.bat && cd C:\Users\me\Desktop\test && apt-cel-convert.exe --format text --in-place --cel-files CEL_FILE.txt' not found
I've tried different combinations of strucuring the pieces, but the only thing I can get to work is running the .bat file by itself. (shell.exec(script) works.)
Is what I am trying to do possible in R?
EDIT: I am able to get my desired result if I manually code this. Instead of opening a window, the output is displayed in R studio. (Needed shot paths for the system() command):
system('"C:\\PROGRA~1\\THERMO~1\\AFFYME~1\\APT-12~1.5\\bin\\apt-vars.bat" && cd "C:\\Users\\me\\Desktop\\test" && apt-cel-convert.exe --f text --i --cel-files CEL_FILE.txt')
But when I try to roll everything up with variables and paste0() I can't get it to work. The line returns without error, but it doesn't trigger the script.
#set apt location
apt_cnvrt <- "C:/Program Files/Thermo Fisher Scientific/Affymetrix Power Tools/APT-1.20.5/bin/apt-vars.bat"
#convert path format
apt_cnvrt <- gsub("/", "\\\\", apt_cnvrt)
#get short path
apt_cnvrt <- shortPathName(apt_cnvrt)
#store wd
outdir <- getwd()
#create file with list of files in dir
cel_list <- list.files(path = outdir, full.names = F, pattern = ".CEL")
fileConn<-file(paste0(outdir,"/CEL_FILE.txt"))
writeLines(c("cel_files", cel_list), fileConn)
close(fileConn)
#convert path format
outdir <- gsub("/", "\\\\", outdir)
#get short path
outdir <- shortPathName(outdir)
#set apt command
command <- "apt-cel-convert.exe --f text --i --cel-files CEL_FILE.txt"
#paste variables together to make: system('"C:\\Progra~1\\Thermo~1\\Affyme~1\\APT-1.20.5\\bin\\apt-vars.bat" && cd "C:\\Users\\me\\Desktop\\test" && apt-cel-convert.exe --format text --in-place --cel-files CEL_FILE.txt')
path <- noquote(paste0("'\"",apt_cnvrt,"\"", " && cd \"",outdir,"\" && ", command,"'"))
system(path)
Path looks like this when all is said and done, and it matches the command that works exactly....but it won't trigger the script:
'"C:\\PROGRA~1\\THERMO~1\\AFFYME~1\\APT-12~1.5\\bin\\apt-vars.bat" && cd "C:\\Users\\me\\Desktop\\test" && apt-cel-convert.exe --f text --i --cel-files CEL_FILE.txt'
*This isn't a duplicate, it's the initial question. As I made progress and had a different question, I asked a new question and linked to it.