Upload an Entire Directory through SFTP with R

July 28, 2019

I wanted to speed up the process of uploading this website to the hosting server, as opposed to doing a lot of drag and drop to the ftp client. It turned out to be super easy using R. If you need to do the same, the following code can help get you there. It will replace old files and directories and create new ones as needed.

Simply set your working directory to the root directory that holds the folders/files you want to upload. Then do the following:

  • Replace User with your username
  • Replace Password with your password
  • Replace FTPServer with the server name
  • Replace your_directory_name with the root directory on the server you want to update
  • Run the code below and you are golden.
library(RCurl)

files_for_upload <- list.files(getwd(), 
                             full.names=F,
                             recursive = TRUE)

for (file_for_upload in files_for_upload) {
  print(file_for_upload)
  ftpUpload(file_for_upload, paste0("sftp://User:Password@FTPServer/your_directory_name/",
                                              file_for_upload),
                      .opts = list(ftp.create.missing.dirs=TRUE)
            )
}
comments powered by Disqus