【问题标题】:Check if a file is open using Windows command line within R检查文件是否在 R 中使用 Windows 命令行打开
【发布时间】:2014-09-29 15:31:43
【问题描述】:

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

【问题讨论】:

  • 切换到不会阻塞的体面的 PDF 阅读器。我使用 SumatraPDF。
  • 我也用这个,但这是给其他使用 Adob​​e 的人的。

标签: r cmd knitr sweave


【解决方案1】:

我并不是说这是一个很好的解决方案:它很老套,但也许可以。您可以制作该文件的副本并尝试用它覆盖您的原始文件。如果失败,则不会造成任何伤害。如果成功,您将修改文件的信息(而不是内容),但由于您的最终目标是无论如何都要覆盖它,我怀疑这将是一个大问题。无论哪种情况,您都将确定文件是否可以重写。

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

【讨论】:

    【解决方案2】:
    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.
    
    
    .
    --
    

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2013-03-13
      • 2018-02-15
      • 2015-11-28
      • 1970-01-01
      • 2011-10-13
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多