# no datageneration

Question

Using your programming ability check on your computer which folder, from the "Documents" directory (shortcut = ~), has the largest number of files. Display the five folders with the largest number of files on R's prompt.

Solution

doc_folder <- '~' # 'C:/Users/USERNAME/Documents' in Windows
                  # '/home/USERNAME/  in Linux

fct_count_files <- function(dir_in) {
  n_files <- list.files(dir_in, recursive = FALSE)
  return(length(n_files))
}

# be aware this might take lots of time...
all_folders <- fs::dir_ls(path = doc_folder, 
                          type = 'directory', 
                          recurse = TRUE)

counter_files <- sapply(all_folders, fct_count_files)
sorted <- sort(counter_files, decreasing = TRUE)

message('\nThe five folders with highest number of files are:\n\n')
message(paste0(names(sorted[1:5]), collapse = '\n'))

Meta-information

extype: string exsolution: r mchoice2string(c(TRUE, FALSE, FALSE, FALSE, FALSE), single = TRUE) exname: "install pkgs" exshuffle: TRUE



msperlin/afedR documentation built on Sept. 11, 2022, 9:49 a.m.