使用R中的Windows命令行检查文件是否已打开

时间:2014-09-29 15:31:44

标签: r cmd knitr sweave

我正在使用shell()命令从函数中的.tex文件生成pdf文档。此功能有时会使用调整后的数据运行多次,因此会覆盖文档。当然,如果在运行.tex文件时打开pdf文件,则会生成错误,指出它无法运行.tex文件。所以我想知道是否有任何R或Windows cmd命令可以检查文件是否打开?

2 个答案:

答案 0 :(得分:1)

我并没有声称这是一个很好的解决方案:它很黑巧,但也许会这样做。您可以复制该文件并尝试使用它覆盖原始文件。如果失败,则不会造成伤害。如果成功,你已经修改了文件的信息(而不是内容)但是因为你的最终目标是覆盖它,无论如何我怀疑这将是一个巨大的问题。在任何一种情况下,您都可以修复文件是否可以重写。

is.writeable <- function(f) {
    tmp <- tempfile()
    file.copy(f, tmp)
    success <- file.copy(tmp, f)
    return(success)
}

答案 1 :(得分:0)

openfiles /query /v|(findstr /i /c:"C:\Users\David Candy\Documents\Super.xls"&&echo File is open||echo File isn't opened)

输出

592   David Candy     1756     EXCEL.EXE            C:\Users\David Candy\Documents\Super.xls
File is open

Findstr如果找到则返回0,如果找不到则返回1+或错误。

&    seperates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatinate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.


.
--