Skip to content

Instantly share code, notes, and snippets.

@CBSti
Last active June 14, 2023 07:06
Show Gist options
  • Save CBSti/ae0c2921e7986b517db3e6f3e3a4d0ba to your computer and use it in GitHub Desktop.
Save CBSti/ae0c2921e7986b517db3e6f3e3a4d0ba to your computer and use it in GitHub Desktop.
R Function for recursive search of directories queried via the Microsoft365R package
library(Microsoft365R)
library(tidyverse)
list_files_recursive <- function(folder_path) {
files <- drv$list_items(folder_path) %>%
mutate(path = folder_path,
checked = ifelse(isdir == F, T, F))
while(any(files$checked == F)) {
for (id in files[!(files$checked), "id"]) {
print(id)
subfolder_path <- paste0(files[files$id == id, "path"], "/", files[files$id == id, "name"])
files <- bind_rows(files,
drv$list_files(subfolder_path) %>%
mutate(path = subfolder_path,
checked = ifelse(isdir == F, T, F)))
files[files$id == id, "checked"] <- T
}
}
return(files)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment