Skip to content

Instantly share code, notes, and snippets.

@janfait
Last active July 20, 2016 15:04
Show Gist options
  • Save janfait/f28332bf30a45d1cec96c7d8a6ac684d to your computer and use it in GitHub Desktop.
Save janfait/f28332bf30a45d1cec96c7d8a6ac684d to your computer and use it in GitHub Desktop.
searching for a specific pattern on FTP and loading it to a folder
library(stringr)
library(RCurl)
#get current time string
thisMonth <- format(Sys.Date(),"%m")
thisYear <- format(Sys.Date(),"%Y")
timeStamp <- paste0(thisMonth,thisYear)
#build a file pattern to search for by joining cluster names and timestamp
filePattern <- paste(c("EMC","LC"),timeStamp,sep="_",collapse="|")
filePattern
#get the FTP contents
fileNames <- getURL("ftp://ftp-domain.com", userpwd = "user:password", ftp.use.epsv = F,dirlistonly = T)
fileNames <- unlist(str_split(fileNames,"\n"))
#extract files of interest
filesToLoad <- fileNames[str_detect(fileNames,filePattern)]
#if there are any matching files, download them to the server
if(length(filesToLoad)>0){
for(i in filesToLoad){
cmd <- paste0("curl -g ftp://user:[email protected]/",i," -o /where/to/put/the/data/",i)
print(cmd)
system(cmd)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment