floe
floe

Reputation: 417

Download zipped files from ftp via RCurl

I had problems to download zipped files from a ftp server. But now I have solved the problem and because I haven't found any solution to my problem here, I'm sharing my approach. First I tried it with

 download.file()

But there was the problem that my password was ending with an "@". That's why the solution with submittign user and password within the URL wasn't working. The double @ was apparently confusing R.

url <- ftp://user:password@@url

You'll find the solution below. Maybe someone has some improvements.

Maybe for someone it's usefull, Florian

Upvotes: 5

Views: 5443

Answers (2)

Xavier Prudent
Xavier Prudent

Reputation: 1712

If you have no particular reason to stay with Rcurl, you can use this bash-based method:

   URL <- "ftp.server.ca"
   USR <- "aUserName"
   MDP <- "myPassword"
   OUT <- "output.file"
   cmd <- paste("wget -m --ftp-user=",USR," --ftp-password=",MDP, " ftp://", URL," -O ", OUT, sep="")
   system(cmd)

Upvotes: 2

floe
floe

Reputation: 417

Here is my solution:

library(RCurl)

url<- "ftp://adress/"
filenames <- getURL(url, userpwd="USER:PASSWORD", ftp.use.epsv = FALSE, dirlistonly = TRUE) #reading filenames from ftp-server
destnames <- filenames <-  strsplit(filenames, "\r*\n")[[1]] # destfiles = origin file names
con <-  getCurlHandle( ftp.use.epsv = FALSE, userpwd="USER:PASSWORD")
mapply(function(x,y) writeBin(getBinaryURL(x, curl = con, dirlistonly = FALSE), y), x = filenames, y = paste("C:\\temp\\",destnames, sep = "")) #writing all zipped files in one directory

Hopefully for anybody it's usefull! Regards, Florian

Upvotes: 11

Related Questions