Reputation: 356
I have a folder with many subfolder and I want to know: 1. The number of files in each subfolder 2. Whether that number is odd or even (but this is minor)
So, I'm looking for a code/function where the input is the PATH and the output is a table with: SUBDIRECTORY ; # of files ; ODD/EVEN
Suggestions?
Upvotes: 3
Views: 2474
Reputation: 67
nice approach by @user10191355 but only this one is working for me
library(tidyverse)
path <- "~/folder/"
tibble(f = paste0(path,"/",dir(path))) %>%
rowwise() %>%
mutate(n = dir(f) %>% length)
this comes in handy if you are interested in subdirectories as well
library(stringr)
tibble(f= paste0(path,"/",dir(path,recursive=TRUE,include.dirs=TRUE))) %>%
filter(!str_detect(f,"\\.")) %>%
rowwise() %>%
mutate(n = dir(f) %>% length)
Upvotes: 0
Reputation:
Here's a tidyverse option where I create a tibble (data frame) with a single variable subfolder
containing the subfolder names. Then I mutate
, adding file_n
by iterating over the combined path
+ subfolder
in str_glue
, passing to dir
and getting the length of the returned vector with length
. In the final step I simply return TRUE
if file_n
is even, and FALSE
otherwise, in variable is_even
:
library(tidyverse)
path <- "~/folder/"
tibble(subfolder = dir(path)) %>%
mutate(file_n = map_int(str_glue("{path}{subfolder}"), ~ dir(.) %>% length),
is_even = file_n %% 2 == 0
)
# A tibble: 3 x 3
subfolder file_n is_even
<chr> <int> <lgl>
1 subfolder1 1 FALSE
2 subfolder2 2 TRUE
3 subfolder3 3 FALSE
Upvotes: 1
Reputation: 12430
Base R
comes with a handy function for this called list.files
. Using the pattern argument you can narrow down your search ("."
gives you all files). Using all.files = TRUE
you could also include hidden files.
folder <- "C:/Users/Johannes Gruber/Pictures"
files <- list.files(folder, pattern = ".", all.files = FALSE, recursive = TRUE, full.names = TRUE)
# number of all files
length(files)
#> [1] 182
You can use split
to split up this vector into a list with the content of each folder in separate list elements.
# 1. The number of files in each subfolder
dir_list <- split(files, dirname(files))
files_in_folder <- sapply(dir_list, length)
head(files_in_folder)
#> C:/Users/Pictures
#> 10
#> C:/Users/Pictures/2019/2019-12-30
#> 3
#> C:/Users/Pictures/2019/2019-12-31
#> 9
#> C:/Users/Pictures/2020/2020-01-01
#> 6
#> C:/Users/Pictures/2020/2020-01-03
#> 2
#> C:/Users/Pictures/2020/2020-01-04
#> 26
# 2. Whether that number is odd or even (but this is minor)
even <- sapply(files_in_folder, function(x) x %% 2 == 0)
head(even)
#> C:/Users/Pictures
#> TRUE
#> C:/Users/Pictures/2019/2019-12-30
#> FALSE
#> C:/Users/Pictures/2019/2019-12-31
#> FALSE
#> C:/Users/Pictures/2020/2020-01-01
#> TRUE
#> C:/Users/Pictures/2020/2020-01-03
#> TRUE
#> C:/Users/Pictures/2020/2020-01-04
#> TRUE
Upvotes: 6