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 list.files
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 list.files
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.
To list all files and sort them by size, use the -S option. By default, it displays output in descending order (biggest to smallest in size). You can output the file sizes in human-readable format by adding the -h option as shown. And to sort in reverse order, add the -r flag as follows.
You may have to use download. file() and then check the file size locally. Since R 3.2 there's a file. size() wrapper.
To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.
Using the ls Command–l – displays a list of files and directories in long format and shows the sizes in bytes.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With