0

I am downloading files from a password secured ftp. At home it works, but not at the Uni, where I need it, since I am using that computer for running scheduled tasks.
The code example is as follows (no using real names):

require("RCurl")         
url_fore<- "ftp://xx.ftp./MET/"
    local_fore <- "./data/ECMWF/"
    file_fore <- try(getURL(url_fore, userpwd = userpwd,dirlistonly = TRUE, crlf=TRUE ))
    file_fore <- unlist(str_extract_all(file_fore, ".+(.csv)"))  

And I get the listing, but when trying to download files that do not exist locally I get an error.
I have found an example function online and adapted it for my case.

require("threadr")
    downloadCSV <- function(filename, folder, baseurl) {
      fileurl <- paste0(baseurl, filename)
      if (!file.exists(str_c(folder, "/", filename))) {
        download_ftp_file(fileurl,
                          paste0(folder, "/", filename),
                          credentials = userpwd)
        Sys.sleep(1)
      }
    }    
lapply(file_fore, function (x) downloadCSV(x, local_fore,url_fore))   


 Error in download.file(url, file_local, quiet = !verbose) : 
  cannot open URL 'ftp://xx.ftp.mu.ie/MET/FORECAST/some_file.csv' In addition: Warning message:
In download.file(url, file_local, quiet = !verbose) :
  InternetOpenUrl failed: 'The login request was denied`  

I have tried this, first solution did not help, and I did not know how to try second one, since that function is depreciated. Same question as mine here did not receive any answer.

Thanks...

m_c
  • 496
  • 2
  • 19
  • It looks to me that you are using one FTP library (RCurl) for listing, while another library (`download_ftp_file`) for the download. Why? If you got RCurl working for the listing, why don't you use it for the download? - https://stackoverflow.com/q/22235421/850848 - In any case, you should tell us, what libraries are you using, and not make us guess. – Martin Prikryl Jun 12 '17 at 12:13
  • I have made some changes. I have used different function because I have found it online and does the job for me, or at least at home computer. – m_c Jun 12 '17 at 13:47
  • Did you try the RCurl code from the question I've linked above? – Martin Prikryl Jun 12 '17 at 13:54
  • 1
    I just came home to test it again and it works, this is very strange. I have tried now to us RCurl, and it works, I just need to figure out how to download a .csv file, because I get only a character vector with the content of a file. I am using `getURL(paste0(url_fore, tail(file_fore, 1)),userpwd = userpwd)` – m_c Jun 12 '17 at 14:02

0 Answers0