Shekhar
unread,Oct 27, 2011, 10:12:15 AM10/27/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Bangalore R Users - BRU
Hi all,
i recently got involved in downloading 1000 files from a FTP site, and
the problem was you cannot download the directory containing the
files.You have to manually download the entire 1000 files, which
myself or for that matters nobody wants to do this.
Here is present R code where you can download the files from an FTP
site. The site requires the username and password as well.
library(RCurl)
# FTP
# Download the files within a directory.
url = 'ftp://usernamel:password@sitename/directory_containing_files/'
filenames = getURL(url, ftp.use.epsv = FALSE, dirlistonly = TRUE)
# Deal with newlines as \n or \r\n. (BDR)
# Or alternatively, instruct libcurl to change \n's to \r\n's for us
with crlf = TRUE
# filenames = getURL(url, ftp.use.epsv = FALSE, ftplistonly = TRUE,
crlf = TRUE)
filenames = paste(url, strsplit(filenames, "\r*\n")[[1]], sep = "")
con = getCurlHandle( ftp.use.epsv = FALSE)
# there is a slight possibility that some of the files that are
# returned in the directory listing and in filenames will disappear
# when we go back to get them. So we use a try() in the call getURL.
contents = sapply(filenames[1:length(filenames)], function(x)
try(getURL(x, curl = con)))
names(contents) = filenames[1:length(contents)]
#extracting the filenames from the names(contents)
#Used to extract the exact file names from the URL
file_name<-unlist(lapply(names(contents),function(i){substring(i,
53)}))
#creating the file names
physicalFileName<-
unlist(lapply(seq_along(1:length(file_name)),function(i){paste("./
News/",file_name[i],sep="",collapse="")}))
for(i in 1:length(contents))
{
file.create(physicalFileName[i])
write(contents[i],physicalFileName[i])
}
Regards,
Som Shekhar