Tuesday, April 7, 2015

How to Auto-Download a CSV File From a Password Protected Website

The Challenge

I’d like to automatically download a csv file from a password protected site daily.

Step 1

I first attempted to use read.table but this would not work because the userid is my email address and contains a @. Therefore, I needed to use browseURL.
  ## browseURL("http://myEmail@work.com:myPsswrd@website.com/myFile.csv", 
  ##      browser = "C:/Users/Path/Local/Google/Chrome/Application/chrome.exe")
This willl automatically download my csv file to whatever local folder I have designated for Chrome downloads.

Step 2

My file has been downloaded, but now I want to copy the original into a new folder and delete the original.
  ## myData <- read.csv("myFile.csv")
  ## setwd("C:/Path/To/New/Folder")
  ## write.table(myData,"newFile.csv")
  ## setwd("C:/Path/To/Old Download/Folder")
  ## remove.file("myFile.csv")

Step 3

Next we save the combined scripts above as autoDownload.R and schedule the script to run daily in Windows Task Scheduler.
  • Open Task Scheduler and Create a new task
  • Under Action tab: use path to Rscript.exe in Program/Script field
  • Use autoDownload.R in the Add Attributes field
  • Use path to autoDownload.R in the Start in field

Complete

No comments:

Post a Comment