29

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.

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) 
Alexander
  • 624
  • 2
  • 7
  • 18
  • 1
    I don't have an FTP server to test it with, but could you use list.files to grab all of the file names, and then parse it for the most recent? – Christie Haskell Marsh Mar 06 '14 at 21:04
  • Thank you for the suggestion. That should work. However, being quite new to R I am not sure how I would approach that. – Alexander Mar 06 '14 at 21:19
  • possible duplicate of [Recursively ftp download, then extract gz files](http://stackoverflow.com/questions/5227444/recursively-ftp-download-then-extract-gz-files) – sds Mar 06 '14 at 22:20

1 Answers1

36

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

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") 
Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • 1
    How do i get latest file based on file attribute `modified time` instead of file name itself? – sjd Aug 13 '20 at 12:03