0

How to check if a file exist using sftp in Remote server using R.

Out of 5 files, 2 are missing in the server. hence it reads first two files and then throw the following error: Error in function (type, msg, asError = TRUE) : Could not open remote file for reading: No such file or directory The program breaks as the error is obtained and does not read the three available files.

library("RCurl")
protocol <- "sftp"
server <- "data.google.com"
userpwd <- "xyz:xyz123"
fileName <- "abc.csv"
url <- paste0(protocol, "://", server, "/", fileName)
data <- getURL(url = url, userpwd = userpwd)
Khaynes
  • 1,976
  • 2
  • 15
  • 27
LoopBloke
  • 15
  • 4
  • Is this in a for loop, i.e trying to obtain each file, one-by-one? – Khaynes Jan 04 '19 at 22:24
  • yes, I got the list of certain files and iterating the names using a for loop. – LoopBloke Jan 04 '19 at 22:39
  • 2
    you can just use the `tryCatch` function. So something like: tryCatch(getURL(url = url, userpwd=userpwd), error = function(e) FALSE). It will return FALSE if it doesn't exists, otherwise it will return the file contents. It's hard to see what you're aiming to achieve without including the actual for-loop. – Khaynes Jan 04 '19 at 22:47
  • It worked, it worked yes..!! Thank you soo much!! "function(e) FALSE" I did not know you could use it like that – LoopBloke Jan 04 '19 at 22:54
  • I have a similar setup but with a different server, except that this time I am downloading .xls files instead of just csv. I don't know why but I am getting the following error Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) : embedded nul in string: – LoopBloke Dec 27 '21 at 21:10

0 Answers0