R/unlink2.R

Defines functions unlink2

Documented in unlink2

#' Delete files and directories
#'
#' @export
#' @note Updated 2022-06-08.
#'
#' @details
#' This variant hardens file path handling, for better Windows compatibility.
#'
#' @param x `character`.
#' Files or directories to be deleted.
#' Unlink base `unlink`, these must exist on disk or the function will
#' intentionally error.
#'
#' @return `integer(1)`.
#' `0` for success, `1` for failure, invisibly.
#'
#' @examples
#' tempdir <- tempdir2()
#' x <- file.path(tempdir, "file.txt")
#' y <- file.path(tempdir, "directory")
#' invisible(file.create(x))
#' invisible(dir.create(y))
#' out <- unlink2(c(x, y))
#' print(out)
unlink2 <- function(x) {
    x <- normalizePath(
        path = x,
        winslash = "\\",
        mustWork = TRUE
    )
    out <- unlink(x, recursive = TRUE)
    assert(!any(file.exists(x)))
    invisible(out)
}
acidgenomics/r-acidbase documentation built on Jan. 12, 2024, 3:56 a.m.