使用 R 从 ftp-server 下载最新文件 [英] Using R to download newest files from ftp-server

查看:32
本文介绍了使用 R 从 ftp-server 下载最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多文件名

FileA2014-03-05-10-24-12
FileB2014-03-06-10-25-12

其中2014-03-05-10-24-12"部分的意思是年/日/月/时/分/秒/".这些文件驻留在 ftp 服务器上.我想使用 R 连接到 ftp 服务器并根据日期下载最新的文件.

Where the part "2014-03-05-10-24-12" means "Year/Day/Month/Hours/Minutes/Seconds/". These files reside on a ftp-server. I would like to use R to connect to the ftp-server and download whatever file is newest based on date.

我已经开始尝试使用 RCurl 和 dirlistonly 列出内容.下一步将是尝试解析并找到最新的文件.还没到……

I have started trying to list the content, using RCurl and dirlistonly. Next step will be to try to parse and find the newest file. Not quite there yet...

library(RCurl)
getURL("ftpserver/",verbose=TRUE,dirlistonly = TRUE) 

推荐答案

这应该可行

library(RCurl)
url <- "ftp://yourServer"
userpwd <- "yourUser:yourPass"
filenames <- getURL(url, userpwd = userpwd,
             ftp.use.epsv = FALSE,dirlistonly = TRUE) 

-

times<-lapply(strsplit(filenames,"[-.]"),function(x){
  time<-paste(c(substr(x[1], nchar(x[1])-3, nchar(x[1])),x[2:6]),
        collapse="-")
  time<-as.POSIXct(time, "%Y-%m-%d-%H-%M-%S", tz="GMT")
})
ind <- which.max(times)
dat <- try(getURL(paste(url,filenames[ind],sep=""), userpwd = userpwd))

所以dat现在包含最新的文件

So datis now containing the newest file

为了使其可复制:所有其他人都可以使用它而不是上部使用

filenames<-c("FileA2014-03-05-10-24-12.csv","FileB2014-03-06-10-25-12.csv") 

这篇关于使用 R 从 ftp-server 下载最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆