使用R从ftp-server下载最新文件

时间:2021-03-02 03:38:13

I have a a number of files named

我有一些名为的文件

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

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.

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

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...

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

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

1 个解决方案

#1


22  

This should work

这应该工作

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

So datis now containing the newest file

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

To make it reproduceable: all others can use this instead of the upper part use

为了使其可再现:所有其他人可以使用它而不是上部使用

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

#1


22  

This should work

这应该工作

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

So datis now containing the newest file

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

To make it reproduceable: all others can use this instead of the upper part use

为了使其可再现:所有其他人可以使用它而不是上部使用

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