Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download zipped files from ftp via RCurl

Tags:

r

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

like image 817
floe Avatar asked Dec 15 '22 00:12

floe


1 Answers

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

like image 112
floe Avatar answered Dec 28 '22 02:12

floe