0

I wish to copy an xlsx file from a really really slow network drive to my RStudio project directory.

Although my file name in my specified file directory is unique, when I run list.files() in that directory I get two versions of the same file. I am assuming the second is a hidden version of the file.

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"

Can anyone suggest a way I can just pick up the second file i.e. the one without ~$

TheGoat
  • 2,587
  • 3
  • 25
  • 58

3 Answers3

3

You could use stringr like this:

library(stringr)
list_of_files[!str_detect(list_of_files, "~$")]
eastclintw00d
  • 2,250
  • 1
  • 9
  • 18
2

You can use grep with fixed, value and invert as 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)

Or str_subset from stringr :

stringr::str_subset(list_of_files, fixed('~$'), negate = TRUE)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Specify a pattern that says your filename is exactly class_xwalk_new.xlsx, not a pattern saying it contains that string. For example, "^class_xwalk_new.xlsx$", so your search would be

list_of_files <- list.files(path = current_folder, 
                            pattern = "^class_xwalk_new.xlsx$", 
                            all.files = FALSE, 
                            recursive = FALSE,
                            full.names = TRUE)
user2554330
  • 37,248
  • 4
  • 43
  • 90