我正在查看pdf文件的数量,并打算将它们转换为文本。我的脚本目前看起来如下:
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)) 这将导致以下消息:
Warning message:
running command '"C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe C:/Folder/Containing/The Test/Pdf.pdf"' had status 127 据我理解,这是因为中间缺少两个双引号,如下所示:
'"C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe**" "**C:/Folder/Containing/The Test/Pdf.pdf"'然而,在花了几个小时的网上,我找不到一个方法,以拟合这两个双引号,而不完全打破表达。运行中的解决方案要么使用cat (我不能这样做,因为我没有尝试输出文本),要么使用ANSII,R直接引用了ANSII,而没有根据需要对其进行转换。真希望能在这里找到一些答案。
编辑:最后的解决方案只需要在最后一行进行更改。转换后的线条如下所示:
sapply(myfiles, function(i) system2("C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe", args =paste0('"',i,'"',collapse = ""), wait = FALSE)) 谢谢你的帮助!
发布于 2018-02-04 07:02:11
错误似乎发生在使用system2命令打开文件的方式上。实际上,文件的名称可以作为args传递给函数system2。
修改后的代码可以是:
sapply(myfiles, function(i) system2(
"C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe",
args = i, wait = FALSE)) 发布于 2018-02-04 07:02:02
system2允许您提供传递给系统命令的附加参数。下列办法可能有效:
sapply(myfiles, function(i) system2("C:/Program Files/Wondershare/PDFelement 6 Professional/PDFelement.exe", args = i, wait = FALSE))https://stackoverflow.com/questions/48605183
复制相似问题