【问题标题】:Ignore hidden files when using list.files in R [duplicate]在R中使用list.files时忽略隐藏文件[重复]
【发布时间】:2020-07-04 09:49:54
【问题描述】:

我希望将 xlsx 文件从非常慢的网络驱动器复制到我的 RStudio 项目目录。

虽然我在指定文件目录中的文件名是唯一的,但当我在该目录中运行 list.files() 时,我会得到同一个文件的两个版本。我假设第二个是文件的隐藏版本。

current_folder <- "//server/address1/address2/address3/Info/more/and more/and more again/R Code"

new_folder <- getwd()

list_of_files <- list.files(path = current_folder, 
                            pattern = "class_xwalk_new.xlsx", 
                            all.files = FALSE, 
                            recursive = FALSE,
                            full.names = TRUE)

> list_of_files
[1] "//server/address1/address2/address3/Info/more/and more/and more again/R Code/~$class_xwalk_new.xlsx"
[2] "//server/address1/address2/address3/Info/more/and more/and more again/R Code/class_xwalk_new.xlsx"

任何人都可以提出一种方法,我可以只拿起第二个文件,即没有 ~$ 的那个

【问题讨论】:

    标签: r


    【解决方案1】:

    你可以像这样使用stringr

    library(stringr)
    list_of_files[!str_detect(list_of_files, "~$")]
    

    【讨论】:

      【解决方案2】:

      您可以将grepfixedvalueinvert 一起用作TRUE

      list_of_files <- c("//server/address1/address2/address3/Info/more/and more/and more again/R Code/~$class_xwalk_new.xlsx",
                         "//server/address1/address2/address3/Info/more/and more/and more again/R Code/class_xwalk_new.xlsx")
      
      grep('~$', list_of_files, fixed = TRUE, value = TRUE, invert = TRUE)
      

      str_subset 来自stringr

      stringr::str_subset(list_of_files, fixed('~$'), negate = TRUE)
      

      【讨论】:

        【解决方案3】:

        指定一个模式,说明您的文件名完全是class_xwalk_new.xlsx,而不是说明它包含该字符串的模式。例如,"^class_xwalk_new.xlsx$",那么您的搜索将是

        list_of_files <- list.files(path = current_folder, 
                                    pattern = "^class_xwalk_new.xlsx$", 
                                    all.files = FALSE, 
                                    recursive = FALSE,
                                    full.names = TRUE)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-06
          • 2012-09-25
          • 2012-11-25
          • 2013-10-20
          • 2015-04-09
          相关资源
          最近更新 更多