Downloading files from ftp with R

2019-02-14 21:33发布

问题:

I am trying to get files from this FTP

ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/

From there, I need only the files starting with the .dat extension. But there are other files that I am not interested in.

I want to avoid downloading each one at a time, so I thought in creating a vector with the names and loop over them.

How can I download only the files I want?

Thanks

EDIT: I have tried doing the following

downloadURL <- "ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/F010439.dat"
download.file(downloadURL, "F010439.dat") #this is a trial using one file

And after a few seconds I get the following error:

trying URL 

    'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/F010439.dat'
    Error in download.file(downloadURL, "F010439.dat") : 
      cannot open URL 'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/F010439.dat'
    In addition: Warning message:
    In download.file(downloadURL, "F010439.dat") :
      InternetOpenUrl failed: 'Die FTP-Sitzung wurde beendet.

'

回答1:

Use the curl library to extract the directory listing

> library(curl)
> url = "ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/11/PXD000299/"
> h = new_handle(dirlistonly=TRUE)
> con = curl(url, "r", h)
> tbl = read.table(con, stringsAsFactors=TRUE, fill=TRUE)
> close(con)
> head(tbl)
                                                 V1
1  12-0210_Druart_Uterus_J0N-Co_1a_ORBI856.raw.mzML
2  12-0210_Druart_Uterus_J0N-Co_2a_ORBI857.raw.mzML
3  12-0210_Druart_Uterus_J0N-Co_3a_ORBI858.raw.mzML
4 12-0210_Druart_Uterus_J10N-Co_1a_ORBI859.raw.mzML
5 12-0210_Druart_Uterus_J10N-Co_2a_ORBI860.raw.mzML
6 12-0210_Druart_Uterus_J10N-Co_3a_ORBI861.raw.mzML

Paste the relevant ones on to the url and use

urls <- paste0(url, tbl[1:5,1])
fls = basename(urls)
curl_fetch_disk(urls[1], fls[1])