Simple code to repeatedly download and unarchive


I would like to get a simple code to download DBs and unarchive them repeatedly.

Here is what I currently have:

 # Attribution of download links 
DB2021<- ftp://link1.7z
DB2020<- ftp://link2.7z
DB2019<- ftp://link3.7z

# Creating a temporary file
tf <- tempfile()

# Downloading the DB to the temporary file and unarchiving
download.file(DB2021, tf,mode="wb",quiet=T) 
archive_extract(tf, dir="DB2021") 

download.file(DB2020, tf,mode="wb",quiet=T)
archive_extract(tf, dir="DB2020")

download.file(DB2019, tf,mode="wb",quiet=T)
archive_extract(tf, dir="DB2019") 

I would like to avoid repeating the last two lines as many times as there are DBs

That's why I thought of something like:

download.file(BD20i, tf,mode="wb",quiet=T)
archive_extract(tf, dir="BD20i")
#for i from 00 to 22.

Do you have any ideas for getting something simple and non-repetitive?

Hi there,

I don't have a perfect way to help you with the reprex but you can essentially create a loop like this:

DB_list <- c("ftp://link1.7z","ftp://link2.7z","ftp://link3.7z")

# Creating a temporary file
tf <- tempfile()

#create some way to keep track of the number
j <- 0

for (i in DB_list){

  #increase j
  j <- j +1

 #download the corresponding file based on DB_list
  download.file(i, tf,mode="wb",quiet=T) 
 #archive extract based on name provided
  archive_extract(tf, dir= paste0("BD20_",j)) 

You will list all the urls together in a vector and then you will loop over them. I think something close to this will work for your situation.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.