Search code examples
rcmddouble-quotes

Trying to keep double quotes inside the command prompt request from R


I am going through the number of pdf files with an intention to convert them to text. My script currently looks like this:

setwd("test")
dest<-getwd()

#creating vector 

myfiles <- list.files(path = dest, pattern = "pdf",  full.names = TRUE)

#removing spaces from file names

sapply(myfiles, FUN = function(i){
  file.rename(from = i, to =  paste0(dirname(i), "/", gsub(" ", "", basename(i))))
})

#recreating vectors to renamed files

myfiles <- list.files(path = dest, pattern = "pdf",  full.names = TRUE)

#starting converter

sapply(myfiles, function(i) system2(paste("C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe",i), wait = FALSE)) 

Which results in the following message:

Warning message:
running command '"C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe C:/Folder/Containing/The Test/Pdf.pdf"' had status 127 

Which, as to my understanding, is caused by lack of two double quotes in the middle like so:

'"C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe**" "**C:/Folder/Containing/The Test/Pdf.pdf"'

However, after spending hours online, I cannot find a way of fitting those two double quotes in without breaking the expression completely. The running solutions are either use cat, which I cannot do, as I am not trying to output text, or using ANSII, which R quotes directly without transforming it as necessary. Really hoping to find some answers here.

EDIT: the final solution required change in the last line only. The transformed line looks like so:

sapply(myfiles, function(i) system2("C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe", args =paste0('"',i,'"',collapse = ""), wait = FALSE)) 

Thank you for the help!


Solution

  • The error seems to be in the way system2 command is used to open the file. Actually the name of the file can be passed as args to function system2.

    The modified code can be:

    sapply(myfiles, function(i) system2(
         "C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe",
          args = i, wait = FALSE))