简体   繁体   English

R使用Rcurl从FTP下载多个文件

[英]R Downloading multiple file from FTP using Rcurl

I'm a new R. user. 我是R.的新用户。 I am trying to download 7.000 files(.nc format) from ftp server ( which I got from user and password). 我正在尝试从ftp服务器上下载7.000个文件(.nc格式)(我是从用户名和密码那里获得的)。 On the website, each file is a link to download. 在网站上,每个文件都是下载链接。 I would like to download all the files (.nc). 我想下载所有文件(.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. 我感谢任何可以帮助我如何在R中运行这些作业的人。仅举一个示例,我尝试使用Rcurl和一个循环进行操作并通知我:无法下载所有文件。

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? 我怎样才能做到这一点?

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. 我建议您使用curl软件包而不是Rcurl来下载文件,因为为每个下载请求提供身份验证信息会更容易。

    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)
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM