Search code examples
rftprcurl

Connection problem FTP using RCurl in R - specific folder


When I first start R Studio I cannot get the following code to run.

library ("RCurl")

userpwd <- "User:Passwd"
tsfrFilename <- "/Path/*.his"    ### there is only one .his file in this pathway ###
ouptFilename <- "output.csv"

url <- "ftp://12.34.56.78/Path/*.his" 
data <- getURL(url = url, userpwd=userpwd)

Returns this error:

Error in function (type, msg, asError = TRUE)  : RETR response: 550

However, after specifying the specific path and file name (as per below) it works a charm. But, when I subsequently use the original code above, I am able to connect no worries and transfer the file.

userpwd <- "User:Passwd"
tsfrFilename <- "/Path/next/next/name.his"
ouptFilename <- "output.csv"

url <- "ftp://12.34.56.78/Path/next/next/name.his" 
data <- getURL(url = url, userpwd=userpwd)

The original code without specified pathway/name works until I end the R session, then same problem upon restart.

Any ideas why this is happening? I cannot specify the specific folder or file name as it is overwritten every 30 minutes and assigned into folders for each year/month, thus I need the *.his to work straight away.

Thanks in advance.


Solution

  • This is what I ended up doing. I had to create a dynamic IP address using the date format as shown below.

    # Get the date parts we need
    Year <-format(Sys.Date(), format="%Y")
    Month <- format(Sys.Date(), format="%B")
    MM <- format(Sys.Date(), format="%m")
    
    # Create the file string and read
    emupark <- glue::glue("ftp://user:passwd@99.99.99.99/path/path/{Year}/{Month}/site}{Year}-{MM}.csv")
    sitename<- read.csv(sitename, header = FALSE)