R Downloading multiple file from FTP using Rcurl

拈花ヽ惹草 提交于 2019-12-12 05:05:25

问题


I'm a new R. user. I am trying to download 7.000 files(.nc format) from ftp server ( which I got from user and password). On the website, each file is a link to download. I would like to download all the files (.nc).

I thank anyone who can help me how to run those jobs in R. Just an example what I have tried to do using Rcurl and a loop and informs me: cannot download all files.

library(RCurl)

url<- "ftp://ftp.my.link.fr/1234/"
userpwd <- userpwd="user:password"
destination <- "/Users/ME/Documents"
filenames <- getURL(url, userpwd="user:password", 
ftp.use.epsv = FALSE, dirlistonly = TRUE)

for(i in seq_along(url)){
  download.file(url[i], destination[i], mode="wb")
}

how can I do that?


回答1:


The first thing you'd see is that the files in your directory, ie the object filenames, would be listed as one long string. To obtain an object of all file names as a character vector, you may try:

    files <- unlist(strsplit(filenames, '\n'))

From here on, it's simply a matter of looping through all the files in the directory. I recommend you use the curl package, not Rcurl, to download the files, as it's easier to supply auth info for every download request.

    library(curl)
    h <- new_handle()
    handle_setopt(h, userpwd = "user:pwd")

and then

    lapply(files, function(filename){
    curl_download(paste(url, filename, sep = ""), destfile = filename, handle = h)
    })


来源:https://stackoverflow.com/questions/39705379/r-downloading-multiple-file-from-ftp-using-rcurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!