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

时间:2021-04-06 02:05:59

I'm using the console of PDFSam to split and merge some PDF files. I can do this semi-automatically using .bat files, but I would like to do the whole thing in R.

我正在使用PDFSam控制台分割和合并一些PDF文件。我可以使用。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? I've tried a few of the options listed in ?shell to no avail.

我的shell命令中是否缺少一个选项?我尝试过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

谢谢,汤姆

2 个解决方案

#1


4  

You could pass multiple commands to shell by concatenating them by &, so below should work:

通过将多个命令连接到shell,您可以将它们连接到shell中,因此下面应该工作:

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')

#2


0  

This isn't exactly an answer to your question, but you might try system("youBatFile.bat") as an alternative.

这并不是你问题的答案,但是你可以试试system(“youBatFile.bat”)作为替代。

#1


4  

You could pass multiple commands to shell by concatenating them by &, so below should work:

通过将多个命令连接到shell,您可以将它们连接到shell中,因此下面应该工作:

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')

#2


0  

This isn't exactly an answer to your question, but you might try system("youBatFile.bat") as an alternative.

这并不是你问题的答案,但是你可以试试system(“youBatFile.bat”)作为替代。