Last active
July 20, 2016 15:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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