0

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.

Steve
  • 588
  • 4
  • 17
  • 1
    A possible option is to edit the bat file to change the directory, or create a new bat file to change the directory and then have it call your apt_vars.bat file. – Dave2e Oct 29 '19 at 15:52
  • Possible duplicate of [R system or system2 command using paste or sprintf to run bat file?](https://stackoverflow.com/questions/58616143/r-system-or-system2-command-using-paste-or-sprintf-to-run-bat-file) – Dave2e Nov 04 '19 at 14:14

1 Answers1

0

I attempted to post a more concise question with my updates here:

[R system or system2 command using paste or sprintf to run bat file?

And I was able to get this to work by calling system as follows:

system(sprintf("\"%s\" && cd \"%s\" && %s", apt_cnvrt, outdir, command))
Steve
  • 588
  • 4
  • 17