R/getFTPFileList.R

Defines functions getFTPFileList

Documented in getFTPFileList

#' Fetch File List
#'
#' This function allows you to pull down a list of files within an
#' FTP directory.
#'
#' @param host The FTP host url.
#' @param userpwd login username and password colon seperated.
#' @param stringSearch RegEx search for filenames in the directory.
#' @param fileType Takes list that designates "zip", "csv", or both.
#' @return Returns an array of file names in the directory.
#' @export


##TODO: Write test cases for function:
# 1) Multiple files
# 2) Singular File
# 3) Just csv
# 4) Just zip
##TODO: Add option to pull back all files in directory, i.e. NULL stringSearch parameter.

#### Get list of files from an FTP directory ####
getFTPFileList <- function(host, userpwd, stringSearch, fileType = c("zip", "csv")) {

  #do a quick check for traling "/"
  host <- ifelse(grepl("\\/$", host), host, paste0(host,"/"))

  x <- RCurl::getURL(host, userpwd = userpwd,
              ftp.use.epsv = F, dirlistonly = F)
  x <- strsplit(x, "\r*\n")[[1]]
  x <- unlist(strsplit(x = x, "\\s"))
  x <- x[apply(sapply(fileType, function(z)
    grepl(sprintf("\\b%s\\b", z), x = x)), 1, function(y)
      Reduce("|", y))]
  x <- x[grep(stringSearch, x = x)]

  if(length(x) > 1) {

    if (sum(apply(sapply(fileType, function(z)
      grepl(
        sprintf("\\b%s\\b", z), x = x
      )), 2, function(z)
        Reduce("|", z))) > 1) {
      warning("Multiple file types in list.")

    }
  }
  return(x)
}
blazickjoe/DataScienceLibrary documentation built on Nov. 5, 2019, 2:26 p.m.