fileSize.R 987 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #' Get Size of File(s)
  2. #'
  3. #' @description
  4. #' Function for getting size of any files.
  5. #'
  6. #' @param file \code{character} vector of file(s) with path.
  7. #' @param units \code{character}, defaults to "B". Currently available options
  8. #' are "B", "KB", "MB", "GB" or "TB" for bites, kilo-, mega-, giga- and terabytes.
  9. #'
  10. #' @return
  11. #' \code{numeric} vector of the same length as \code{file} (in \code{units}).
  12. #' Note that directories are excluded.
  13. #'
  14. #' @author
  15. #' Matteo Mattiuzzi
  16. #'
  17. #' @examples
  18. #' \dontrun{
  19. #' fileSize(list.files("./"))
  20. #' }
  21. #'
  22. #' @export fileSize
  23. #' @name fileSize
  24. fileSize <- function(file,units = "B") {
  25. units <- toupper(units)
  26. unit <- c(1,1024,1048576,1073741824,1073741824*1024)
  27. names(unit) <- c("B","KB", "MB", "GB","TB")
  28. if (!units %in% names(unit)) {
  29. stop('unit must be one of: "B", "KB", "MB", "GB" or "TB"')
  30. }
  31. file <- file.info(file)
  32. file <- file[!file$isdir,"size"]
  33. res <- file/unit[toupper(units)]
  34. return(res)
  35. }