Automatically Delete Files/Folders

Francesco picture Francesco · Feb 15, 2012 · Viewed 34.7k times · Source

Is there any way to automatically delete all files or folders with few R command lines? I am aware of the unlink() or file.remove() functions, but for those you need to define a character vector with exactly all the names of the files you want to delete. I am looking more for something that lists all the files or folders within a specific path (e.g. 'C:/Temp') and then delete all files with a certain name (regardless of its extension).

Any help is very much appreciated!

Answer

joran picture joran · Feb 15, 2012

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?