Skip to content

Instantly share code, notes, and snippets.

@oousmane
Last active May 10, 2024 10:50
Show Gist options
  • Save oousmane/a78eb6947215f7e3134de4d4dbc61376 to your computer and use it in GitHub Desktop.
Save oousmane/a78eb6947215f7e3134de4d4dbc61376 to your computer and use it in GitHub Desktop.
List the files in a FTP (remote) directory/folder
#' List the files in a FTP (remote) directory/folder
#' @description
#' This function produce a character vector of the names of files in the ftp folder provided
#'
#' @param ftp FTP base directory/folder
#' @param pattern an optional regular expression. Only file names which match the regular expression will be returned.
#' @author Adam H. Sparks, Ousmane Ouedraogo
#'
#' @return character vector of the names of files in FTP base directory
#' @export
#'
#' @examples
#' ftp_base <- "ftp.cpc.ncep.noaa.gov/International/nmme/seasonal_nmme_forecast_in_cpt_format/"
#' ftp_files <- ftp_dir_ls(ftp = ftp_base)
#' ftp_files[1:10]
#'
ftp_dir_ls <- function(ftp, pattern=NULL)
{
list_files <- curl::new_handle()
curl::handle_setopt(
list_files,
ftp_use_epsv = TRUE,
dirlistonly = TRUE
)
con <- curl::curl(
url = ftp,
"r",
handle = list_files)
if (is.null(pattern)){
files <- readLines(con)
} else {
files <- readLines(con)
files <- files[stringr::str_detect(files, pattern = pattern)]
}
close(con)
return(files)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment