Skip to content

Instantly share code, notes, and snippets.

@joelnitta
Last active July 8, 2019 00:56
Show Gist options
  • Save joelnitta/d2efdf778b3c5e0b0ea90f89bca93bc3 to your computer and use it in GitHub Desktop.
Save joelnitta/d2efdf778b3c5e0b0ea90f89bca93bc3 to your computer and use it in GitHub Desktop.
Compare the contents of two folders
#' Compare the contents of two folders
#'
#' Comparison is done by checking file names and the MD5 hash of each
#' file. No recursion is used, so files in subfolders will not be checked.
#'
#' @param x The absolute path to the first folder.
#' @param y The absolut path the second folder.
#'
#' @return A confirmation if all files in the two folders are equal.
#' Otherwise, a list of two tibbles. Each tibble contains a column for
#' file names and a column for the MD5 hash of that file.
#'
compare_folders <- function (x, y) {
require(magrittr)
files_x <- list.files(x, full.names = TRUE) %>%
magrittr::set_names(fs::path_file(.))
hashes_x <- purrr::map_chr(files_x, tools::md5sum)
files_y <- list.files(y, full.names = TRUE) %>%
magrittr::set_names(fs::path_file(.))
hashes_y <- purrr::map_chr(files_y, tools::md5sum)
if(isTRUE(all.equal(hashes_x, hashes_y))) return (message("All files equal"))
warning("Files not equal")
list(
hash_table_x = tibble::tibble(
file_x = names(hashes_x),
hash_x = hashes_x
),
hash_table_y = tibble::tibble(
file_y = names(hashes_y),
hash_y = hashes_y
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment