EcologyTom EcologyTom - 4 months ago 14
R Question

list.files taking account of file size in R?

I have a large number of files in several folders. I can get a list of these files with;

MY_FILES <- list.files(WORKING_DIRECTORY, pattern = "MY_PATTERN", recursive = TRUE)

Most, but not all, of the files are larger than 50Mb. How can I modify the
call, so that MY_FILES only contains those above the 50Mb threshold? Or do I need another step to subset MY_FILES afterwards? (Not sure how to do this because
returns a vector of names only, there are no details about the files)

I need to stick to R because this is only one step in a series of data manipulations. Thanks.


Sure, just get file sizes.

x <- list.files(full.names = TRUE)

x[sapply(x, file.size) > 300000]
[1] "./hami.jpg"          "./process_steps.jpg" "./shp_sveta.png"

Here I subset only files which are bigger than 300kB. Notice that atom.jpg and other smaller files are not included in the subset. You should use full.names argument to access files which are not in getwd().

enter image description here