Home > Software engineering >  R download.file to a folder keeping original name
R download.file to a folder keeping original name

Time:11-25

I'm trying to download a single file with a GET request URL that doesn't specify the filename of the returned file. It returns zip files if you request multiple filter items, but single xlsx files if you only have one filter item. I want to download the file into a folder then find out its name, rather than save it into a tempfile with a random name.

# web GET request
web <- "https://www.find-school-performance-data.service.gov.uk/download-data?download=true&regions=0&filters=GIAS&fileformat=xls&year=2021-2022&meta=false"

tf <- tempfile()
td <- tempdir()

# this works fine, but gives the file a random name
download.file(web, tf, mode="wb")

#these don't work as it wants me to give the full file name, not just the folder
download.file(web, td, mode="wb")
download.file(web, paste0(td,"\\"), mode="wb")

Warning messages:
1: In download.file(web, paste0(td, "\\"), mode = "wb") :
  URL https://www.find-school-performance-data.service.gov.uk/download-data?download=true&regions=0&filters=GIAS&fileformat=xls&year=2021-2022&meta=false: cannot open destfile 'C:\Users\USER\AppData\Local\Temp\RtmpWi98JC\', reason 'No such file or directory'
2: In download.file(web, paste0(td, "\\"), mode = "wb") :
  download had nonzero exit status

CodePudding user response:

using httr we can fetch the name of the file being returned. This means a double GET request, but it works

library(httr)

td <- tempdir()
web <- "https://www.find-school-performance-data.service.gov.uk/download-data?download=true&regions=0&filters=GIAS&fileformat=xls&year=2021-2022&meta=false"

# load header response from web GET request
hdr <- HEAD(web)
filename <- gsub(".*name=", "", headers(hdr)$`content-disposition`)

filename [1] "2021-2022/england_school_information.xlsx"

# download to temp directory using original name
download.file(web, paste0(td,"\\",filename), mode="wb")

CodePudding user response:

I'm not sure how you can do this with R, but if you paste the link into your browser address bar it'll save it in a folder with a file name. 2021-2022_england_school_information.xlsx

  • Related