Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files from a FTP site requiring credentials using R

Tags:

r

ftp

I am trying to download a few zip files from a FTP site which requires credentials. I was able to get the list of files from the site using the getURL function in the RCurl pacakge. Here is the code I used (from the answer to one of the old stackoverflow questions).

library(RCurl)
url<-"ftp://ftp.mysite.com/EOD/"
userpwd<-"name:password"
filenames <- getURL(url, userpwd = userpwd,
                    ftp.use.epsv = FALSE,dirlistonly = TRUE)
filenames<-strsplit(filenames, "\r\n")

I have tried several functions (downloadFile, download.file, getURL, etc.) to download these files to a local directory on my computer however, I keep getting the error below-

Error in download.file(url, destfile = pathnameT, mode = mode, quiet = !isVisible(verbose),  : 
  cannot open URL 'ftp://ftp.mysite.com/EOD/testfile_txt.zip'
In addition: Warning message:
In download.file(url, destfile = pathnameT, mode = mode, quiet = !isVisible(verbose),  :
  InternetOpenUrl failed: 'The login request was denied

After reading about this error, I tries two things- 1. specify setInternet2(use=TRUE) in downloadFile function; and 2. Uncheck "Use Internet Explorer library/proxy for HTTP" in global tools > Pacakages.

I'll appreciate if someone could help me with downloading these zipped files to a local directory using R. Thank you.

like image 524
asmi Avatar asked Mar 24 '17 14:03

asmi


1 Answers

Interesting. I changed the argument ftp.use.epsv = FALSE to ftp.use.epsv = TRUE and worked fine.

Another change that make more organized the response:

from filenames<-strsplit(filenames, "\r\n")

to: filenames<-strsplit(filenames, "\n")

like image 173
ROBBAT1 Avatar answered Oct 09 '22 03:10

ROBBAT1