Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export(read_datalogger_file)
export(read_file_dropbox)
export(read_sapflow_file)
export(read_teros_file)
export(scan_folders)
import(dplyr)
import(fpeek)
importFrom(dplyr,bind_rows)
Expand Down
36 changes: 0 additions & 36 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,6 @@ calculate_skip <- function(filename, header_rows, min_timestamp,
}


#' Scan sub-folders
#'
#' @param root_dir Root directory to start from, character
#' @param file_pattern Regex for files, character
#' @param quiet Be quiet or print diagnostic messages? Logical
#'
#' @return A named list of folder contents; each list object name is the
#' name of the folder, and each object in the list is a vector of
#' fully qualified filenames.
#' @note Does not recurse into sub-folders.
#' @export
#'
#' @examples
#' scan_folders("./")
scan_folders <- function(root_dir, file_pattern = "\\.csv$", quiet = TRUE) {
if(!dir.exists(root_dir)) {
stop("Directory doesn't exist!")
}
entries <- list.files(root_dir, full.names = TRUE)

folder_list <- list()
for(e in entries) {
if(!quiet) message(e)
if(dir.exists(e)) { # it's a folder
files <- list.files(e, pattern = file_pattern, full.names = TRUE)
if(!quiet) message("\t", length(files), " files")
if(length(files)) { # ...with csv files!
folder_list[[e]] <- files
}
}
}

return(folder_list)
}


#' A recursive function to print a nicely-formatted directory tree and its files
#'
#' @param dir_list A list of directories
Expand Down
29 changes: 0 additions & 29 deletions man/scan_folders.Rd

This file was deleted.

56 changes: 0 additions & 56 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,59 +38,3 @@ test_that("calculate_skip works", {
suppressWarnings(calculate_skip(tf, 1, "2022-05-01", quiet = TRUE)),
"Could not parse")
})


test_that("scan_folders works", {
# Nonexistent parent folder
expect_error(scan_folders("folder_doesnt_exist"), "doesn't exist")

# Empty folder
td <- file.path(tempdir(), "test")
dir.create(td)
expect_identical(scan_folders(td), list())
# Folder with files, no subfolders
file.create(file.path(td, "test.csv"))
expect_identical(scan_folders(td), list())
# Empty subfolder
a <- file.path(td, "a")
dir.create(a)
expect_identical(scan_folders(td), list())
# File in a folder!
fn <- file.path(td, "a", "test.csv")
file.create(fn)
x <- scan_folders(td)
expect_identical(length(x), 1L)
expect_identical(basename(names(x)), "a")
expect_identical(normalizePath(dirname(names(x))), normalizePath(td))
expect_identical(basename(x[[1]]), "test.csv")
expect_identical(normalizePath(dirname(x[[1]])), normalizePath(a))
# Adding a non-csv file shouldn't change anything
file.create(file.path(td, "a", "test.png"))
expect_identical(scan_folders(td), x)
# Second file
file.create(file.path(td, "a", "test2.csv"))
y <- scan_folders(td)
expect_identical(length(y), 1L)
expect_identical(length(y[[1]]), 2L)
expect_identical(basename(y[[1]]), c("test.csv", "test2.csv"))
# Second folder
b <- file.path(td, "b")
dir.create(b)
expect_identical(scan_folders(td), y) # no new files so no change
file.create(file.path(td, "b", "test3.csv"))
z <- scan_folders(td)
expect_identical(length(z), 2L)
expect_identical(z[[1]], y[[1]]) # no change to first entry
expect_identical(basename(names(z)[2]), "b")
expect_identical(length(z[[2]]), 1L)
expect_identical(basename(z[[2]]), "test3.csv")

# Respects file pattern
expect_identical(scan_folders(td, file_pattern = "kjb"), list())

# Generates messages with asked
expect_silent(scan_folders(td, quiet = TRUE))
suppressMessages({
expect_message(scan_folders(td, quiet = FALSE), "files")
})
})
Loading