简体   繁体   English

使用R中的shell()运行.bat文件

[英]Run .bat file using shell() in R

I'm using the console of PDFSam to split and merge some PDF files. 我正在使用PDFSam的控制台来拆分和合并一些PDF文件。 I can do this semi-automatically using .bat files, but I would like to do the whole thing in R. 我可以使用.bat文件半自动执行此操作,但我想在R中完成所有操作。

This code in my .bat file works: 我的.bat文件中的此代码有效:

C:
cd "/Program Files/pdfsam/bin/"
run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split

But this "equivalent" code in my R shell command returns no errors, but doesn't seem to work. 但是我的R shell命令中的这个“等效”代码没有返回错误,但似乎不起作用。

shell('C: 
cd "/Program Files/pdfsam/bin/"
run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')

Is there an option I'm missing in my shell command? 我的shell命令中是否有一个选项? I've tried a few of the options listed in ?shell to no avail. 我已经尝试了一些在shell中列出的选项无济于事。

I'm using windows XP and R version 2.13.1 (2011-07-08) Platform: i386-pc-mingw32/i386 (32-bit) 我正在使用Windows XP和R版本2.13.1(2011-07-08)平台:i386-pc-mingw32 / i386(32位)

Thanks, Tom 谢谢,汤姆

You could pass multiple commands to shell by concatenating them by & , so below should work: 你可以通过多个命令将通过连接它们外壳& ,所以下面应该工作:

shell('C: & cd C:/Program Files/pdfsam/bin & run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')

But as a workaround you could temporary change R working directory: 但作为一种解决方法,您可以临时更改R工作目录:

current.wd <- getwd()
setwd("C:/Program Files/pdfsam/bin")
shell('run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')
setwd(current.wd)

If you do it frequent write a function: 如果你经常写一个函数:

shell.in.dir <- function(dir, ...) {
    current.wd <- getwd()
    on.exit(setwd(current.wd))
    setwd(dir)
    shell(...)
}

shell.in.dir("C:/Program Files/pdfsam/bin",
    'run-console.bat -f "d:/delete/A_9.pdf" -o d:/delete -s BURST -overwrite split')

这不是您问题的答案,但您可以尝试使用system("youBatFile.bat")作为替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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