Home > Enterprise >  For a list of folders remove all the subfolders that match list of pattern with R
For a list of folders remove all the subfolders that match list of pattern with R

Time:06-28

I'm looking for a way to remove all subfolders matching a list of patterns (c('*_003','*_007','*_011','*_012')) contained in several folders. I've come up with this function but unfortunately nothing happens after I run it :/

Any hints would be greatly appreciated ! :)

myfunction <- function(folders_list) {
  for(dir in Sys.glob(folders_list)) {
    files.to.keep <- c('*_003','*_007','*_011','*_012')
    files.in.dir <- list.files(pattern = paste0(files.to.keep), recursive = TRUE)
    files.to.remove <- list(files.in.dir[!(files.in.dir %in% grep(paste(files.to.keep,collapse = "|"), files.in.dir, value=TRUE))])
    do.call(unlink, files.to.remove)
  }}

myfunction('C:/Users/Users/myfolder/project/report_201*/')

CodePudding user response:

You had a few issues here but the main one was this line:

files.in.dir <- list.files(pattern = paste0(files.to.keep), recursive = TRUE)

You were not passing the directory passed as an argument to the function to list.files() so it was returning the files in the working directory.

You could try this:

myfunction <- function(folders_list) {
  for(each_dir in Sys.glob(folders_list)) {
    files_to_keep_pattern  <- paste(c('*_003','*_007','*_011','*_012'), collapse = "|")
    files_in_dir  <- dir(each_dir, recursive = TRUE, full.names = TRUE)
    files_to_delete  <- files_in_dir[!grepl(files_to_keep_pattern, files_in_dir)]
    message("Deleting ", length(files_to_delete), " files")
    print(files_to_delete)
    #do.call(unlink, files_to_delete)
}}

Having said that - I would be extremely careful selecting which files to delete with a glob pattern. I have in fact commented out the line which does the deletion and just made the function print the files it is going to delete. Uncomment the last line if you are really sure you want to do this...

  •  Tags:  
  • r
  • Related