Automatically Delete Files/Folders Automatically Delete Files/Folders r r

Automatically Delete Files/Folders


Maybe you're just looking for a combination of file.remove and list.files? Maybe something like:

do.call(file.remove, list(list.files("C:/Temp", full.names = TRUE)))

And I guess you can filter the list of files down to those whose names match a certain pattern using grep or grepl, no?


For all files in a known path you can:

unlink("path/*")


dir_to_clean <- tempdir() #or wherever#create some junk to test it withfile.create(file.path(  dir_to_clean,   paste("test", 1:5, "txt", sep = ".")))#Now remove them (no need for messing about with do.call)file.remove(dir(    dir_to_clean,   pattern = "^test\\.[0-9]\\.txt$",   full.names = TRUE))

You can also use unlink as an alternative to file.remove.